| 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}} int strToInt(const char*, bool*); | |
| 23 } // namespace internal | |
| 24 | |
| 25 template <typename T, typename C> | |
| 26 class {{config.class_export.macro}} String16Base { | |
| 27 public: | |
| 28 static bool isASCII(C c) | |
| 29 { | |
| 30 return !(c & ~0x7F); | |
| 31 } | |
| 32 | |
| 33 static bool isSpaceOrNewLine(C c) | |
| 34 { | |
| 35 return isASCII(c) && c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); | |
| 36 } | |
| 37 | |
| 38 static T fromInteger(int number) | |
| 39 { | |
| 40 char buffer[50]; | |
| 41 internal::intToStr(number, buffer, PROTOCOL_ARRAY_LENGTH(buffer)); | |
| 42 return T(buffer); | |
| 43 } | |
| 44 | |
| 45 static T fromDouble(double number) | |
| 46 { | |
| 47 char buffer[100]; | |
| 48 internal::doubleToStr(number, buffer, PROTOCOL_ARRAY_LENGTH(buffer)); | |
| 49 return T(buffer); | |
| 50 } | |
| 51 | |
| 52 static T fromDoublePrecision3(double number) | |
| 53 { | |
| 54 char buffer[100]; | |
| 55 internal::doubleToStr3(number, buffer, PROTOCOL_ARRAY_LENGTH(buffer)); | |
| 56 return T(buffer); | |
| 57 } | |
| 58 | |
| 59 static T fromDoublePrecision6(double number) | |
| 60 { | |
| 61 char buffer[100]; | |
| 62 internal::doubleToStr6(number, buffer, PROTOCOL_ARRAY_LENGTH(buffer)); | |
| 63 return T(buffer); | |
| 64 } | |
| 65 | |
| 66 static int charactersToInteger(const C* characters, size_t length, bool* ok
= nullptr) | |
| 67 { | |
| 68 std::vector<char> buffer; | |
| 69 buffer.reserve(length + 1); | |
| 70 for (size_t i = 0; i < length; ++i) { | |
| 71 if (!isASCII(characters[i])) { | |
| 72 if (ok) | |
| 73 *ok = false; | |
| 74 return 0; | |
| 75 } | |
| 76 buffer.push_back(static_cast<char>(characters[i])); | |
| 77 } | |
| 78 buffer.push_back('\0'); | |
| 79 return internal::strToInt(buffer.data(), ok); | |
| 80 } | |
| 81 | |
| 82 int toInteger(bool* ok = nullptr) const | |
| 83 { | |
| 84 const C* characters = static_cast<const T&>(*this).characters16(); | |
| 85 size_t length = static_cast<const T&>(*this).length(); | |
| 86 return charactersToInteger(characters, length, ok); | |
| 87 } | |
| 88 | |
| 89 T stripWhiteSpace() const | |
| 90 { | |
| 91 size_t length = static_cast<const T&>(*this).length(); | |
| 92 if (!length) | |
| 93 return T(); | |
| 94 | |
| 95 unsigned start = 0; | |
| 96 unsigned end = length - 1; | |
| 97 const C* characters = static_cast<const T&>(*this).characters16(); | |
| 98 | |
| 99 // skip white space from start | |
| 100 while (start <= end && isSpaceOrNewLine(characters[start])) | |
| 101 ++start; | |
| 102 | |
| 103 // only white space | |
| 104 if (start > end) | |
| 105 return T(); | |
| 106 | |
| 107 // skip white space from end | |
| 108 while (end && isSpaceOrNewLine(characters[end])) | |
| 109 --end; | |
| 110 | |
| 111 if (!start && end == length - 1) | |
| 112 return T(static_cast<const T&>(*this)); | |
| 113 return T(characters + start, end + 1 - start); | |
| 114 } | |
| 115 }; | |
| 116 | |
| 117 } // namespace protocol | |
| 118 } // namespace blink | |
| 119 | |
| 120 #include "{{config.lib.string16_header}}" | |
| 121 | |
| 122 namespace blink { | |
| 123 namespace protocol { | |
| 124 | |
| 125 class {{config.class_export.macro}} String16Builder { | |
| 126 public: | |
| 127 String16Builder(); | |
| 128 void append(const String16&); | |
| 129 void append(UChar); | |
| 130 void append(char); | |
| 131 void append(const UChar*, size_t); | |
| 132 void append(const char*, size_t); | |
| 133 String16 toString(); | |
| 134 void reserveCapacity(size_t); | |
| 135 | |
| 136 private: | |
| 137 std::vector<UChar> m_buffer; | |
| 138 }; | |
| 139 | |
| 140 } // namespace protocol | |
| 141 } // namespace blink | |
| 142 | |
| 143 using String16 = blink::protocol::String16; | |
| 144 using String16Builder = blink::protocol::String16Builder; | |
| 145 | |
| 146 #endif // !defined(String16_h) | |
| OLD | NEW |