| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 1 | // Copyright 2016 the V8 project 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 V8_INSPECTOR_STRINGUTIL_H_ |
| 6 | #define V8_INSPECTOR_STRINGUTIL_H_ |
| 7 | |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 8 | #include <memory> |
| 9 | |
| 10 | #include "src/base/logging.h" |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 11 | #include "src/base/macros.h" |
| 12 | #include "src/inspector/string-16.h" |
| 13 | |
| 14 | #include "include/v8-inspector.h" |
| 15 | |
| 16 | namespace v8_inspector { |
| 17 | |
| 18 | namespace protocol { |
| 19 | |
| 20 | class Value; |
| 21 | |
| 22 | using String = v8_inspector::String16; |
| 23 | using StringBuilder = v8_inspector::String16Builder; |
| 24 | |
| 25 | class StringUtil { |
| 26 | public: |
| 27 | static String substring(const String& s, size_t pos, size_t len) { |
| 28 | return s.substring(pos, len); |
| 29 | } |
| 30 | static String fromInteger(int number) { return String::fromInteger(number); } |
| 31 | static String fromInteger(size_t number) { |
| 32 | return String::fromInteger(number); |
| 33 | } |
| 34 | static String fromDouble(double number) { return String::fromDouble(number); } |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 35 | static size_t find(const String& s, const char* needle) { |
| 36 | return s.find(needle); |
| 37 | } |
| 38 | static size_t find(const String& s, const String& needle) { |
| 39 | return s.find(needle); |
| 40 | } |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 41 | static const size_t kNotFound = String::kNotFound; |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 42 | static void builderAppend(StringBuilder& builder, const String& s) { |
| 43 | builder.append(s); |
| 44 | } |
| 45 | static void builderAppend(StringBuilder& builder, UChar c) { |
| 46 | builder.append(c); |
| 47 | } |
| 48 | static void builderAppend(StringBuilder& builder, const char* s, size_t len) { |
| 49 | builder.append(s, len); |
| 50 | } |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 51 | static void builderReserve(StringBuilder& builder, size_t capacity) { |
| 52 | builder.reserveCapacity(capacity); |
| 53 | } |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 54 | static String builderToString(StringBuilder& builder) { |
| 55 | return builder.toString(); |
| 56 | } |
| 57 | static std::unique_ptr<protocol::Value> parseJSON(const String16& json); |
| 58 | static std::unique_ptr<protocol::Value> parseJSON(const StringView& json); |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 61 | } // namespace protocol |
| 62 | |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 63 | v8::Local<v8::String> toV8String(v8::Isolate*, const String16&); |
| 64 | v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const String16&); |
| 65 | v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const char*); |
| 66 | v8::Local<v8::String> toV8String(v8::Isolate*, const StringView&); |
| 67 | // TODO(dgozman): rename to toString16. |
| 68 | String16 toProtocolString(v8::Local<v8::String>); |
| 69 | String16 toProtocolStringWithTypeCheck(v8::Local<v8::Value>); |
| 70 | String16 toString16(const StringView&); |
| 71 | StringView toStringView(const String16&); |
| 72 | bool stringViewStartsWith(const StringView&, const char*); |
| 73 | |
| 74 | class StringBufferImpl : public StringBuffer { |
| 75 | public: |
| 76 | // Destroys string's content. |
| 77 | static std::unique_ptr<StringBufferImpl> adopt(String16&); |
| 78 | const StringView& string() override { return m_string; } |
| 79 | |
| 80 | private: |
| 81 | explicit StringBufferImpl(String16&); |
| 82 | String16 m_owner; |
| 83 | StringView m_string; |
| 84 | |
| 85 | DISALLOW_COPY_AND_ASSIGN(StringBufferImpl); |
| 86 | }; |
| 87 | |
| 88 | } // namespace v8_inspector |
| 89 | |
| 90 | #endif // V8_INSPECTOR_STRINGUTIL_H_ |