string-util.h 2.85 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_INSPECTOR_STRINGUTIL_H_
#define V8_INSPECTOR_STRINGUTIL_H_

8 9 10
#include <memory>

#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 17 18 19 20 21 22 23 24 25 26

namespace v8_inspector {

namespace protocol {

class Value;

using String = v8_inspector::String16;
using StringBuilder = v8_inspector::String16Builder;

class StringUtil {
 public:
27
  static String substring(const String& s, size_t pos, size_t len) {
28 29 30
    return s.substring(pos, len);
  }
  static String fromInteger(int number) { return String::fromInteger(number); }
31 32 33
  static String fromInteger(size_t number) {
    return String::fromInteger(number);
  }
34
  static String fromDouble(double number) { return String::fromDouble(number); }
35
  static double toDouble(const char* s, size_t len, bool* isOk);
36 37 38 39 40 41
  static size_t find(const String& s, const char* needle) {
    return s.find(needle);
  }
  static size_t find(const String& s, const String& needle) {
    return s.find(needle);
  }
42
  static const size_t kNotFound = String::kNotFound;
43 44 45 46 47 48 49 50 51
  static void builderAppend(StringBuilder& builder, const String& s) {
    builder.append(s);
  }
  static void builderAppend(StringBuilder& builder, UChar c) {
    builder.append(c);
  }
  static void builderAppend(StringBuilder& builder, const char* s, size_t len) {
    builder.append(s, len);
  }
52
  static void builderReserve(StringBuilder& builder, size_t capacity) {
53 54
    builder.reserveCapacity(capacity);
  }
55 56 57
  static String builderToString(StringBuilder& builder) {
    return builder.toString();
  }
58 59
  static std::unique_ptr<protocol::Value> parseJSON(const String16& json);
  static std::unique_ptr<protocol::Value> parseJSON(const StringView& json);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
};

}  // namespace protocol

v8::Local<v8::String> toV8String(v8::Isolate*, const String16&);
v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const String16&);
v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const char*);
v8::Local<v8::String> toV8String(v8::Isolate*, const StringView&);
// TODO(dgozman): rename to toString16.
String16 toProtocolString(v8::Local<v8::String>);
String16 toProtocolStringWithTypeCheck(v8::Local<v8::Value>);
String16 toString16(const StringView&);
StringView toStringView(const String16&);
bool stringViewStartsWith(const StringView&, const char*);

class StringBufferImpl : public StringBuffer {
 public:
  // Destroys string's content.
  static std::unique_ptr<StringBufferImpl> adopt(String16&);
  const StringView& string() override { return m_string; }

 private:
  explicit StringBufferImpl(String16&);
  String16 m_owner;
  StringView m_string;
85 86

  DISALLOW_COPY_AND_ASSIGN(StringBufferImpl);
87 88 89 90 91
};

}  //  namespace v8_inspector

#endif  // V8_INSPECTOR_STRINGUTIL_H_