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 <memory> 9 10 #include "src/base/logging.h" 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); } 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 } 41 static const size_t kNotFound = String::kNotFound; 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 } 51 static void builderReserve(StringBuilder& builder, size_t capacity) { 52 builder.reserveCapacity(capacity); 53 } 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); 59 }; 60 61 } // namespace protocol 62 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_ 91