| 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 #ifndef String16_h | |
| 6 #define String16_h | |
| 7 | |
| 8 //#include "Collections.h" | |
| 9 //#include "Platform.h" | |
| 10 #include "{{config.class_export.header}}" | |
| 11 | |
| 12 #include <vector> | |
| 13 | |
| 14 namespace blink { | |
| 15 namespace protocol { | |
| 16 | |
| 17 namespace internal { | |
| 18 {{config.class_export.macro}} void intToStr(int, char*, size_t); | |
| 19 {{config.class_export.macro}} void doubleToStr(double, char*, size_t); | |
| 20 {{config.class_export.macro}} void doubleToStr3(double, char*, size_t); | |
| 21 {{config.class_export.macro}} void doubleToStr6(double, char*, size_t); | |
| 22 {{config.class_export.macro}} double strToDouble(const char*, bool*); | |
| 23 {{config.class_export.macro}} int strToInt(const char*, bool*); | |
| 24 } // namespace internal | |
| 25 | |
| 26 template <typename T, typename C> | |
| 27 class {{config.class_export.macro}} String16Base { | |
| 28 public: | |
| 29 static bool isASCII(C c) | |
| 30 { | |
| 31 return !(c & ~0x7F); | |
| 32 } | |
| 33 | |
| 34 static bool isSpaceOrNewLine(C c) | |
| 35 { | |
| 36 return isASCII(c) && c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); | |
| 37 } | |
| 38 | |
| 39 static T fromInteger(int number) | |
| 40 { | |
| 41 char buffer[50]; | |
| 42 internal::intToStr(number, buffer, PROTOCOL_ARRAY_LENGTH(buffer)); | |
| 43 return T(buffer); | |
| 44 } | |
| 45 | |
| 46 static T fromDouble(double number) | |
| 47 { | |
| 48 char buffer[100]; | |
| 49 internal::doubleToStr(number, buffer, PROTOCOL_ARRAY_LENGTH(buffer)); | |
| 50 return T(buffer); | |
| 51 } | |
| 52 | |
| 53 static T fromDoublePrecision3(double number) | |
| 54 { | |
| 55 char buffer[100]; | |
| 56 internal::doubleToStr3(number, buffer, PROTOCOL_ARRAY_LENGTH(buffer)); | |
| 57 return T(buffer); | |
| 58 } | |
| 59 | |
| 60 static T fromDoublePrecision6(double number) | |
| 61 { | |
| 62 char buffer[100]; | |
| 63 internal::doubleToStr6(number, buffer, PROTOCOL_ARRAY_LENGTH(buffer)); | |
| 64 return T(buffer); | |
| 65 } | |
| 66 | |
| 67 static double charactersToDouble(const C* characters, size_t length, bool* o
k = nullptr) | |
| 68 { | |
| 69 std::vector<char> buffer; | |
| 70 buffer.reserve(length + 1); | |
| 71 for (size_t i = 0; i < length; ++i) { | |
| 72 if (!isASCII(characters[i])) { | |
| 73 if (ok) | |
| 74 *ok = false; | |
| 75 return 0; | |
| 76 } | |
| 77 buffer.push_back(static_cast<char>(characters[i])); | |
| 78 } | |
| 79 buffer.push_back('\0'); | |
| 80 return internal::strToDouble(buffer.data(), ok); | |
| 81 } | |
| 82 | |
| 83 static int charactersToInteger(const C* characters, size_t length, bool* ok
= nullptr) | |
| 84 { | |
| 85 std::vector<char> buffer; | |
| 86 buffer.reserve(length + 1); | |
| 87 for (size_t i = 0; i < length; ++i) { | |
| 88 if (!isASCII(characters[i])) { | |
| 89 if (ok) | |
| 90 *ok = false; | |
| 91 return 0; | |
| 92 } | |
| 93 buffer.push_back(static_cast<char>(characters[i])); | |
| 94 } | |
| 95 buffer.push_back('\0'); | |
| 96 return internal::strToInt(buffer.data(), ok); | |
| 97 } | |
| 98 | |
| 99 double toDouble(bool* ok = nullptr) const | |
| 100 { | |
| 101 const C* characters = static_cast<const T&>(*this).characters16(); | |
| 102 size_t length = static_cast<const T&>(*this).length(); | |
| 103 return charactersToDouble(characters, length, ok); | |
| 104 } | |
| 105 | |
| 106 int toInteger(bool* ok = nullptr) const | |
| 107 { | |
| 108 const C* characters = static_cast<const T&>(*this).characters16(); | |
| 109 size_t length = static_cast<const T&>(*this).length(); | |
| 110 return charactersToInteger(characters, length, ok); | |
| 111 } | |
| 112 | |
| 113 T stripWhiteSpace() const | |
| 114 { | |
| 115 size_t length = static_cast<const T&>(*this).length(); | |
| 116 if (!length) | |
| 117 return T(); | |
| 118 | |
| 119 unsigned start = 0; | |
| 120 unsigned end = length - 1; | |
| 121 const C* characters = static_cast<const T&>(*this).characters16(); | |
| 122 | |
| 123 // skip white space from start | |
| 124 while (start <= end && isSpaceOrNewLine(characters[start])) | |
| 125 ++start; | |
| 126 | |
| 127 // only white space | |
| 128 if (start > end) | |
| 129 return T(); | |
| 130 | |
| 131 // skip white space from end | |
| 132 while (end && isSpaceOrNewLine(characters[end])) | |
| 133 --end; | |
| 134 | |
| 135 if (!start && end == length - 1) | |
| 136 return T(static_cast<const T&>(*this)); | |
| 137 return T(characters + start, end + 1 - start); | |
| 138 } | |
| 139 | |
| 140 bool startsWith(const char* prefix) const | |
| 141 { | |
| 142 const C* characters = static_cast<const T&>(*this).characters16(); | |
| 143 size_t length = static_cast<const T&>(*this).length(); | |
| 144 for (size_t i = 0, j = 0; prefix[j] && i < length; ++i, ++j) { | |
| 145 if (characters[i] != prefix[j]) | |
| 146 return false; | |
| 147 } | |
| 148 return true; | |
| 149 } | |
| 150 }; | |
| 151 | |
| 152 } // namespace protocol | |
| 153 } // namespace blink | |
| 154 | |
| 155 #include "{{config.lib.string16_header}}" | |
| 156 | |
| 157 namespace blink { | |
| 158 namespace protocol { | |
| 159 | |
| 160 class {{config.class_export.macro}} String16Builder { | |
| 161 public: | |
| 162 String16Builder(); | |
| 163 void append(const String16&); | |
| 164 void append(UChar); | |
| 165 void append(char); | |
| 166 void append(const UChar*, size_t); | |
| 167 void append(const char*, size_t); | |
| 168 String16 toString(); | |
| 169 void reserveCapacity(size_t); | |
| 170 | |
| 171 private: | |
| 172 std::vector<UChar> m_buffer; | |
| 173 }; | |
| 174 | |
| 175 } // namespace protocol | |
| 176 } // namespace blink | |
| 177 | |
| 178 using String16 = blink::protocol::String16; | |
| 179 using String16Builder = blink::protocol::String16Builder; | |
| 180 | |
| 181 #endif // !defined(String16_h) | |
| OLD | NEW |