OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "platform/inspector_protocol/String16.h" |
| 6 |
| 7 #include <cstdio> |
| 8 #include <cstdlib> |
| 9 #include <cstring> |
| 10 #include <string> |
| 11 |
| 12 namespace blink { |
| 13 namespace protocol { |
| 14 |
| 15 bool isASCIISpace(UChar c) |
| 16 { |
| 17 return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); |
| 18 } |
| 19 |
| 20 bool isSpaceOrNewLine(UChar c) |
| 21 { |
| 22 return c <= 0x7F ? isASCIISpace(c) : false; |
| 23 } |
| 24 |
| 25 String16 string16FromInteger(int number) |
| 26 { |
| 27 static char buffer[50]; |
| 28 std::sprintf(buffer, "%d", number); |
| 29 return String16(buffer); |
| 30 } |
| 31 |
| 32 String16 string16FromDouble(double number) |
| 33 { |
| 34 static char buffer[100]; |
| 35 std::sprintf(buffer, "%f", number); |
| 36 return String16(buffer); |
| 37 } |
| 38 |
| 39 String16 string16FromDoublePrecision3(double number) |
| 40 { |
| 41 static char buffer[100]; |
| 42 std::sprintf(buffer, "%.3g", number); |
| 43 return String16(buffer); |
| 44 } |
| 45 |
| 46 String16 string16FromDoublePrecision6(double number) |
| 47 { |
| 48 static char buffer[100]; |
| 49 std::sprintf(buffer, "%.6g", number); |
| 50 return String16(buffer); |
| 51 } |
| 52 |
| 53 double string16CharactersToDouble(const UChar* characters, size_t length, bool*
ok) |
| 54 { |
| 55 std::string str; |
| 56 str.resize(length); |
| 57 for (size_t i = 0; i < length; ++i) |
| 58 str[i] = static_cast<char>(characters[i]); |
| 59 |
| 60 const char* buffer = str.c_str(); |
| 61 char* endptr; |
| 62 double result = strtod(buffer, &endptr); |
| 63 if (ok) |
| 64 *ok = buffer + length == endptr; |
| 65 return result; |
| 66 } |
| 67 |
| 68 int string16CharactersToInt(const UChar* characters, size_t length, bool* ok) |
| 69 { |
| 70 std::string str; |
| 71 str.resize(length); |
| 72 for (size_t i = 0; i < length; ++i) |
| 73 str[i] = static_cast<char>(characters[i]); |
| 74 |
| 75 const char* buffer = str.c_str(); |
| 76 char* endptr; |
| 77 int result = strtol(buffer, &endptr, 10); |
| 78 if (ok) |
| 79 *ok = buffer + length == endptr; |
| 80 return result; |
| 81 } |
| 82 |
| 83 String16 string16StripWhiteSpace(const String16& source) |
| 84 { |
| 85 if (!source.length()) |
| 86 return String16(); |
| 87 |
| 88 unsigned start = 0; |
| 89 unsigned end = source.length() - 1; |
| 90 |
| 91 // skip white space from start |
| 92 while (start <= end && isSpaceOrNewLine(source[start])) |
| 93 ++start; |
| 94 |
| 95 // only white space |
| 96 if (start > end) |
| 97 return String16(); |
| 98 |
| 99 // skip white space from end |
| 100 while (end && isSpaceOrNewLine(source[end])) |
| 101 --end; |
| 102 |
| 103 if (!start && end == source.length() - 1) |
| 104 return source; |
| 105 return String16(source.characters16() + start, end + 1 - start); |
| 106 } |
| 107 |
| 108 bool string16StartsWith(const String16& source, const char* prefix) |
| 109 { |
| 110 size_t length = strlen(prefix); |
| 111 if (source.length() < length) |
| 112 return false; |
| 113 for (size_t i = 0; i < length; ++i) { |
| 114 if (source[i] != prefix[i]) |
| 115 return false; |
| 116 } |
| 117 return true; |
| 118 } |
| 119 |
| 120 String16Builder::String16Builder() |
| 121 { |
| 122 } |
| 123 |
| 124 void String16Builder::append(const String16& s) |
| 125 { |
| 126 m_buffer.insert(m_buffer.end(), s.characters16(), s.characters16() + s.lengt
h()); |
| 127 } |
| 128 |
| 129 void String16Builder::append(UChar c) |
| 130 { |
| 131 m_buffer.insert(m_buffer.end(), c); |
| 132 } |
| 133 |
| 134 void String16Builder::append(char c) |
| 135 { |
| 136 UChar u = c; |
| 137 m_buffer.push_back(u); |
| 138 } |
| 139 |
| 140 void String16Builder::append(const UChar* characters, size_t length) |
| 141 { |
| 142 m_buffer.insert(m_buffer.end(), characters, characters + length); |
| 143 } |
| 144 |
| 145 void String16Builder::append(const char* characters, size_t length) |
| 146 { |
| 147 m_buffer.reserve(m_buffer.size() + length); |
| 148 for (size_t i = 0; i < length; ++i, ++characters) { |
| 149 UChar u = *characters; |
| 150 m_buffer.push_back(u); |
| 151 } |
| 152 } |
| 153 |
| 154 String16 String16Builder::toString() |
| 155 { |
| 156 return String16(m_buffer.data(), m_buffer.size()); |
| 157 } |
| 158 |
| 159 void String16Builder::reserveCapacity(size_t capacity) |
| 160 { |
| 161 m_buffer.reserve(capacity); |
| 162 } |
| 163 |
| 164 } // namespace protocol |
| 165 } // namespace blink |
OLD | NEW |