Index: third_party/WebKit/Source/platform/inspector_protocol/String16.cpp |
diff --git a/third_party/WebKit/Source/platform/inspector_protocol/String16.cpp b/third_party/WebKit/Source/platform/inspector_protocol/String16.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d7a1c030a46f90a03b2225058bac0b69348b7f47 |
--- /dev/null |
+++ b/third_party/WebKit/Source/platform/inspector_protocol/String16.cpp |
@@ -0,0 +1,165 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "platform/inspector_protocol/String16.h" |
+ |
+#include <cstdio> |
+#include <cstdlib> |
+#include <cstring> |
+#include <string> |
+ |
+namespace blink { |
+namespace protocol { |
+ |
+bool isASCIISpace(UChar c) |
+{ |
+ return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); |
+} |
+ |
+bool isSpaceOrNewLine(UChar c) |
+{ |
+ return c <= 0x7F ? isASCIISpace(c) : false; |
+} |
+ |
+String16 string16FromInteger(int number) |
+{ |
+ static char buffer[50]; |
+ std::sprintf(buffer, "%d", number); |
+ return String16(buffer); |
+} |
+ |
+String16 string16FromDouble(double number) |
+{ |
+ static char buffer[100]; |
+ std::sprintf(buffer, "%f", number); |
+ return String16(buffer); |
+} |
+ |
+String16 string16FromDoublePrecision3(double number) |
+{ |
+ static char buffer[100]; |
+ std::sprintf(buffer, "%.3g", number); |
+ return String16(buffer); |
+} |
+ |
+String16 string16FromDoublePrecision6(double number) |
+{ |
+ static char buffer[100]; |
+ std::sprintf(buffer, "%.6g", number); |
+ return String16(buffer); |
+} |
+ |
+double string16CharactersToDouble(const UChar* characters, size_t length, bool* ok) |
+{ |
+ std::string str; |
+ str.resize(length); |
+ for (size_t i = 0; i < length; ++i) |
+ str[i] = static_cast<char>(characters[i]); |
+ |
+ const char* buffer = str.c_str(); |
+ char* endptr; |
+ double result = strtod(buffer, &endptr); |
+ if (ok) |
+ *ok = buffer + length == endptr; |
+ return result; |
+} |
+ |
+int string16CharactersToInt(const UChar* characters, size_t length, bool* ok) |
+{ |
+ std::string str; |
+ str.resize(length); |
+ for (size_t i = 0; i < length; ++i) |
+ str[i] = static_cast<char>(characters[i]); |
+ |
+ const char* buffer = str.c_str(); |
+ char* endptr; |
+ int result = strtol(buffer, &endptr, 10); |
+ if (ok) |
+ *ok = buffer + length == endptr; |
+ return result; |
+} |
+ |
+String16 string16StripWhiteSpace(const String16& source) |
+{ |
+ if (!source.length()) |
+ return String16(); |
+ |
+ unsigned start = 0; |
+ unsigned end = source.length() - 1; |
+ |
+ // skip white space from start |
+ while (start <= end && isSpaceOrNewLine(source[start])) |
+ ++start; |
+ |
+ // only white space |
+ if (start > end) |
+ return String16(); |
+ |
+ // skip white space from end |
+ while (end && isSpaceOrNewLine(source[end])) |
+ --end; |
+ |
+ if (!start && end == source.length() - 1) |
+ return source; |
+ return String16(source.characters16() + start, end + 1 - start); |
+} |
+ |
+bool string16StartsWith(const String16& source, const char* prefix) |
+{ |
+ size_t length = strlen(prefix); |
+ if (source.length() < length) |
+ return false; |
+ for (size_t i = 0; i < length; ++i) { |
+ if (source[i] != prefix[i]) |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+String16Builder::String16Builder() |
+{ |
+} |
+ |
+void String16Builder::append(const String16& s) |
+{ |
+ m_buffer.insert(m_buffer.end(), s.characters16(), s.characters16() + s.length()); |
+} |
+ |
+void String16Builder::append(UChar c) |
+{ |
+ m_buffer.insert(m_buffer.end(), c); |
+} |
+ |
+void String16Builder::append(char c) |
+{ |
+ UChar u = c; |
+ m_buffer.push_back(u); |
+} |
+ |
+void String16Builder::append(const UChar* characters, size_t length) |
+{ |
+ m_buffer.insert(m_buffer.end(), characters, characters + length); |
+} |
+ |
+void String16Builder::append(const char* characters, size_t length) |
+{ |
+ m_buffer.reserve(m_buffer.size() + length); |
+ for (size_t i = 0; i < length; ++i, ++characters) { |
+ UChar u = *characters; |
+ m_buffer.push_back(u); |
+ } |
+} |
+ |
+String16 String16Builder::toString() |
+{ |
+ return String16(m_buffer.data(), m_buffer.size()); |
+} |
+ |
+void String16Builder::reserveCapacity(size_t capacity) |
+{ |
+ m_buffer.reserve(capacity); |
+} |
+ |
+} // namespace protocol |
+} // namespace blink |