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_STRING_UTIL_H_ 6 #define V8_INSPECTOR_STRING_UTIL_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: substring(const String & s,size_t pos,size_t len)27 static String substring(const String& s, size_t pos, size_t len) { 28 return s.substring(pos, len); 29 } fromInteger(int number)30 static String fromInteger(int number) { return String::fromInteger(number); } fromInteger(size_t number)31 static String fromInteger(size_t number) { 32 return String::fromInteger(number); 33 } fromDouble(double number)34 static String fromDouble(double number) { return String::fromDouble(number); } 35 static double toDouble(const char* s, size_t len, bool* isOk); find(const String & s,const char * needle)36 static size_t find(const String& s, const char* needle) { 37 return s.find(needle); 38 } find(const String & s,const String & needle)39 static size_t find(const String& s, const String& needle) { 40 return s.find(needle); 41 } 42 static const size_t kNotFound = String::kNotFound; builderAppend(StringBuilder & builder,const String & s)43 static void builderAppend(StringBuilder& builder, const String& s) { 44 builder.append(s); 45 } builderAppend(StringBuilder & builder,UChar c)46 static void builderAppend(StringBuilder& builder, UChar c) { 47 builder.append(c); 48 } builderAppend(StringBuilder & builder,const char * s,size_t len)49 static void builderAppend(StringBuilder& builder, const char* s, size_t len) { 50 builder.append(s, len); 51 } 52 static void builderAppendQuotedString(StringBuilder&, const String&); builderReserve(StringBuilder & builder,size_t capacity)53 static void builderReserve(StringBuilder& builder, size_t capacity) { 54 builder.reserveCapacity(capacity); 55 } builderToString(StringBuilder & builder)56 static String builderToString(StringBuilder& builder) { 57 return builder.toString(); 58 } 59 static std::unique_ptr<protocol::Value> parseJSON(const String16& json); 60 static std::unique_ptr<protocol::Value> parseJSON(const StringView& json); 61 }; 62 63 } // namespace protocol 64 65 v8::Local<v8::String> toV8String(v8::Isolate*, const String16&); 66 v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const String16&); 67 v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const char*); 68 v8::Local<v8::String> toV8String(v8::Isolate*, const StringView&); 69 // TODO(dgozman): rename to toString16. 70 String16 toProtocolString(v8::Isolate*, v8::Local<v8::String>); 71 String16 toProtocolStringWithTypeCheck(v8::Isolate*, v8::Local<v8::Value>); 72 String16 toString16(const StringView&); 73 StringView toStringView(const String16&); 74 bool stringViewStartsWith(const StringView&, const char*); 75 76 class StringBufferImpl : public StringBuffer { 77 public: 78 // Destroys string's content. 79 static std::unique_ptr<StringBufferImpl> adopt(String16&); string()80 const StringView& string() override { return m_string; } 81 82 private: 83 explicit StringBufferImpl(String16&); 84 String16 m_owner; 85 StringView m_string; 86 87 DISALLOW_COPY_AND_ASSIGN(StringBufferImpl); 88 }; 89 90 String16 debuggerIdToString(const std::pair<int64_t, int64_t>& debuggerId); 91 String16 stackTraceIdToString(uintptr_t id); 92 93 } // namespace v8_inspector 94 95 #endif // V8_INSPECTOR_STRING_UTIL_H_ 96