| 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 | |
| 8 | #include "src/base/macros.h" |
| 9 | #include "src/inspector/string-16.h" |
| 10 | |
| 11 | #include "include/v8-inspector.h" |
| 12 | |
| 13 | namespace v8_inspector { |
| 14 | |
| 15 | namespace protocol { |
| 16 | |
| 17 | class Value; |
| 18 | |
| 19 | using String = v8_inspector::String16; |
| 20 | using StringBuilder = v8_inspector::String16Builder; |
| 21 | |
| 22 | class StringUtil { |
| 23 | public: |
| 24 | static String substring(const String& s, size_t pos, size_t len) { |
| 25 | return s.substring(pos, len); |
| 26 | } |
| 27 | static String fromInteger(int number) { return String::fromInteger(number); } |
| 28 | static String fromInteger(size_t number) { |
| 29 | return String::fromInteger(number); |
| 30 | } |
| 31 | static String fromDouble(double number) { return String::fromDouble(number); } |
| 32 | static const size_t kNotFound = String::kNotFound; |
| 33 | static void builderReserve(StringBuilder& builder, size_t capacity) { |
| 34 | builder.reserveCapacity(capacity); |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | std::unique_ptr<protocol::Value> parseJSON(const StringView& json); |
| 39 | std::unique_ptr<protocol::Value> parseJSON(const String16& json); |
| 40 | |
| 41 | } // namespace protocol |
| 42 | |
| 43 | std::unique_ptr<protocol::Value> toProtocolValue(protocol::String* errorString, |
| 44 | v8::Local<v8::Context>, |
| 45 | v8::Local<v8::Value>, |
| 46 | int maxDepth = 1000); |
| 47 | |
| 48 | v8::Local<v8::String> toV8String(v8::Isolate*, const String16&); |
| 49 | v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const String16&); |
| 50 | v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const char*); |
| 51 | v8::Local<v8::String> toV8String(v8::Isolate*, const StringView&); |
| 52 | // TODO(dgozman): rename to toString16. |
| 53 | String16 toProtocolString(v8::Local<v8::String>); |
| 54 | String16 toProtocolStringWithTypeCheck(v8::Local<v8::Value>); |
| 55 | String16 toString16(const StringView&); |
| 56 | StringView toStringView(const String16&); |
| 57 | bool stringViewStartsWith(const StringView&, const char*); |
| 58 | |
| 59 | class StringBufferImpl : public StringBuffer { |
| 60 | public: |
| 61 | // Destroys string's content. |
| 62 | static std::unique_ptr<StringBufferImpl> adopt(String16&); |
| 63 | const StringView& string() override { return m_string; } |
| 64 | |
| 65 | private: |
| 66 | explicit StringBufferImpl(String16&); |
| 67 | String16 m_owner; |
| 68 | StringView m_string; |
| 69 | |
| 70 | DISALLOW_COPY_AND_ASSIGN(StringBufferImpl); |
| 71 | }; |
| 72 | |
| 73 | } // namespace v8_inspector |
| 74 | |
| 75 | #endif // V8_INSPECTOR_STRINGUTIL_H_ |