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
|
|
|
|
|
2019-06-19 15:40:33 +00:00
|
|
|
#include "src/sksl/SkSLDefines.h"
|
2017-09-11 20:50:14 +00:00
|
|
|
#include <cstring>
|
2017-09-11 13:42:09 +00:00
|
|
|
#include <stdarg.h>
|
2019-06-07 16:32:35 +00:00
|
|
|
#include <string>
|
2019-06-06 14:04:27 +00:00
|
|
|
|
2019-09-20 16:19:11 +00:00
|
|
|
#ifndef SKSL_STANDALONE
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#endif
|
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
namespace SkSL {
|
|
|
|
|
2017-09-11 20:50:14 +00:00
|
|
|
// Represents a (not necessarily null-terminated) slice of a string.
|
|
|
|
struct StringFragment {
|
|
|
|
StringFragment()
|
2017-10-26 13:30:08 +00:00
|
|
|
: fChars("")
|
2017-09-11 20:50:14 +00:00
|
|
|
, 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;
|
2018-01-08 20:36:28 +00:00
|
|
|
bool operator<(StringFragment s) const;
|
2017-09-11 20:50:14 +00:00
|
|
|
|
2019-09-20 16:19:11 +00:00
|
|
|
#ifndef SKSL_STANDALONE
|
|
|
|
operator SkString() const { return SkString(fChars, fLength); }
|
|
|
|
#endif
|
|
|
|
|
2017-09-11 20:50:14 +00:00
|
|
|
const char* fChars;
|
|
|
|
size_t fLength;
|
|
|
|
};
|
|
|
|
|
2017-10-13 20:17:45 +00:00
|
|
|
bool operator==(const char* s1, StringFragment s2);
|
|
|
|
|
|
|
|
bool operator!=(const char* s1, StringFragment s2);
|
|
|
|
|
2019-06-18 14:14:20 +00:00
|
|
|
class SK_API String : public std::string {
|
2017-03-31 17:56:23 +00:00
|
|
|
public:
|
2020-08-17 13:47:01 +00:00
|
|
|
using std::string::string;
|
2017-03-31 17:56:23 +00:00
|
|
|
|
2020-08-17 13:47:01 +00:00
|
|
|
String(StringFragment s) : INHERITED(s.fChars, s.fLength) {}
|
2017-09-11 20:50:14 +00:00
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
static String printf(const char* fmt, ...);
|
|
|
|
void appendf(const char* fmt, ...);
|
|
|
|
void vappendf(const char* fmt, va_list va);
|
|
|
|
|
2020-09-16 21:43:11 +00:00
|
|
|
bool startsWith(const char prefix[]) const;
|
|
|
|
bool endsWith(const char suffix[]) const;
|
|
|
|
bool consumeSuffix(const char suffix[]);
|
2018-08-31 14:52:47 +00:00
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
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:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = std::string;
|
2017-03-31 17:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-06-06 14:04:27 +00:00
|
|
|
SKSL_INT stoi(const String& s);
|
|
|
|
SKSL_FLOAT stod(const String& s);
|
2017-08-15 15:14:30 +00:00
|
|
|
long stol(const String& s);
|
2017-03-31 17:56:23 +00:00
|
|
|
|
2019-06-07 16:32:35 +00:00
|
|
|
} // namespace SkSL
|
2017-03-31 17:56:23 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
template<> struct hash<SkSL::String> {
|
|
|
|
size_t operator()(const SkSL::String& s) const {
|
|
|
|
return hash<std::string>{}(s);
|
|
|
|
}
|
|
|
|
};
|
2019-06-07 16:32:35 +00:00
|
|
|
} // namespace std
|
2017-03-31 17:56:23 +00:00
|
|
|
|
|
|
|
#endif
|