2017-03-31 17:56:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SKSL_STRING
|
|
|
|
#define SKSL_STRING
|
|
|
|
|
2017-09-11 20:50:14 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
2017-08-14 18:48:10 +00:00
|
|
|
#define SKSL_USE_STD_STRING
|
2017-03-31 17:56:23 +00:00
|
|
|
|
2017-09-11 13:42:09 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2017-08-14 18:48:10 +00:00
|
|
|
#ifdef SKSL_USE_STD_STRING
|
2017-03-31 17:56:23 +00:00
|
|
|
#define SKSL_STRING_BASE std::string
|
|
|
|
#include <string>
|
|
|
|
#else
|
|
|
|
#define SKSL_STRING_BASE SkString
|
|
|
|
#include "SkString.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace SkSL {
|
|
|
|
|
2017-09-11 20:50:14 +00:00
|
|
|
// Represents a (not necessarily null-terminated) slice of a string.
|
|
|
|
struct StringFragment {
|
|
|
|
StringFragment()
|
|
|
|
: fChars(nullptr)
|
|
|
|
, fLength(0) {}
|
|
|
|
|
|
|
|
StringFragment(const char* chars)
|
|
|
|
: fChars(chars)
|
|
|
|
, fLength(strlen(chars)) {}
|
|
|
|
|
|
|
|
StringFragment(const char* chars, size_t length)
|
|
|
|
: fChars(chars)
|
|
|
|
, fLength(length) {}
|
|
|
|
|
|
|
|
char operator[](size_t idx) const {
|
|
|
|
return fChars[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const char* s) const;
|
|
|
|
bool operator!=(const char* s) const;
|
|
|
|
bool operator==(StringFragment s) const;
|
|
|
|
bool operator!=(StringFragment s) const;
|
|
|
|
|
|
|
|
const char* fChars;
|
|
|
|
size_t fLength;
|
|
|
|
};
|
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
class String : public SKSL_STRING_BASE {
|
|
|
|
public:
|
|
|
|
String() = default;
|
|
|
|
String(const String&) = default;
|
|
|
|
String(String&&) = default;
|
|
|
|
String& operator=(const String&) = default;
|
|
|
|
String& operator=(String&&) = default;
|
|
|
|
|
2017-08-14 18:48:10 +00:00
|
|
|
#ifndef SKSL_USE_STD_STRING
|
2017-03-31 17:56:23 +00:00
|
|
|
String(const SkString& s)
|
|
|
|
: INHERITED(s) {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
String(const char* s)
|
|
|
|
: INHERITED(s) {}
|
|
|
|
|
|
|
|
String(const char* s, size_t size)
|
|
|
|
: INHERITED(s, size) {}
|
|
|
|
|
2017-09-11 20:50:14 +00:00
|
|
|
String(StringFragment s)
|
|
|
|
: INHERITED(s.fChars, s.fLength) {}
|
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
static String printf(const char* fmt, ...);
|
|
|
|
|
2017-08-14 18:48:10 +00:00
|
|
|
#ifdef SKSL_USE_STD_STRING
|
2017-03-31 17:56:23 +00:00
|
|
|
void appendf(const char* fmt, ...);
|
|
|
|
#endif
|
|
|
|
void vappendf(const char* fmt, va_list va);
|
|
|
|
|
|
|
|
bool startsWith(const char* s) const;
|
|
|
|
bool endsWith(const char* s) const;
|
|
|
|
|
|
|
|
String operator+(const char* s) const;
|
|
|
|
String operator+(const String& s) const;
|
2017-09-11 20:50:14 +00:00
|
|
|
String operator+(StringFragment s) const;
|
|
|
|
String& operator+=(char c);
|
|
|
|
String& operator+=(const char* s);
|
|
|
|
String& operator+=(const String& s);
|
|
|
|
String& operator+=(StringFragment s);
|
2017-03-31 17:56:23 +00:00
|
|
|
bool operator==(const char* s) const;
|
|
|
|
bool operator!=(const char* s) const;
|
|
|
|
bool operator==(const String& s) const;
|
|
|
|
bool operator!=(const String& s) const;
|
|
|
|
friend String operator+(const char* s1, const String& s2);
|
|
|
|
friend bool operator==(const char* s1, const String& s2);
|
|
|
|
friend bool operator!=(const char* s1, const String& s2);
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SKSL_STRING_BASE INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
String operator+(const char* s1, const String& s2);
|
|
|
|
bool operator!=(const char* s1, const String& s2);
|
|
|
|
|
|
|
|
String to_string(double value);
|
|
|
|
|
|
|
|
String to_string(int32_t value);
|
|
|
|
|
|
|
|
String to_string(uint32_t value);
|
|
|
|
|
|
|
|
String to_string(int64_t value);
|
|
|
|
|
|
|
|
String to_string(uint64_t value);
|
|
|
|
|
2017-08-15 15:14:30 +00:00
|
|
|
int stoi(const String& s);
|
2017-03-31 17:56:23 +00:00
|
|
|
|
2017-08-15 15:14:30 +00:00
|
|
|
double stod(const String& s);
|
2017-03-31 17:56:23 +00:00
|
|
|
|
2017-08-15 15:14:30 +00:00
|
|
|
long stol(const String& s);
|
2017-03-31 17:56:23 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-09-11 20:50:14 +00:00
|
|
|
namespace std {
|
|
|
|
template<> struct hash<SkSL::StringFragment> {
|
|
|
|
size_t operator()(const SkSL::StringFragment& s) const {
|
|
|
|
size_t result = 0;
|
|
|
|
for (size_t i = 0; i < s.fLength; ++i) {
|
|
|
|
result = result * 101 + s.fChars[i];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2017-08-14 18:48:10 +00:00
|
|
|
#ifdef SKSL_USE_STD_STRING
|
2017-03-31 17:56:23 +00:00
|
|
|
namespace std {
|
|
|
|
template<> struct hash<SkSL::String> {
|
|
|
|
size_t operator()(const SkSL::String& s) const {
|
|
|
|
return hash<std::string>{}(s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
#else
|
|
|
|
#include "SkOpts.h"
|
|
|
|
namespace std {
|
|
|
|
template<> struct hash<SkSL::String> {
|
|
|
|
size_t operator()(const SkSL::String& s) const {
|
|
|
|
return SkOpts::hash_fn(s.c_str(), s.size(), 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
#endif // SKIA_STANDALONE
|
|
|
|
|
|
|
|
#endif
|