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
|
|
|
|
|
2021-03-04 19:30:25 +00:00
|
|
|
#include "include/private/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 {
|
|
|
|
|
2020-10-05 15:49:11 +00:00
|
|
|
class String;
|
|
|
|
|
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) {}
|
|
|
|
|
2020-12-23 23:58:43 +00:00
|
|
|
const char* begin() const { return fChars; }
|
|
|
|
const char* end() const { return fChars + fLength; }
|
|
|
|
|
2020-10-09 16:48:04 +00:00
|
|
|
const char* data() const { return fChars; }
|
|
|
|
size_t size() const { return fLength; }
|
|
|
|
size_t length() const { return fLength; }
|
|
|
|
char operator[](size_t idx) const { return fChars[idx]; }
|
2017-09-11 20:50:14 +00:00
|
|
|
|
2020-11-13 20:55:27 +00:00
|
|
|
bool startsWith(const char prefix[]) const;
|
|
|
|
bool endsWith(const char suffix[]) const;
|
|
|
|
|
2017-09-11 20:50:14 +00:00
|
|
|
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;
|
2020-10-05 15:49:11 +00:00
|
|
|
String operator+(const char* s) const;
|
|
|
|
String operator+(const StringFragment& s) const;
|
|
|
|
String operator+(const String& 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
|
|
|
|
2021-03-18 16:45:15 +00:00
|
|
|
explicit String(std::string s) : INHERITED(std::move(s)) {}
|
2020-08-17 13:47:01 +00:00
|
|
|
String(StringFragment s) : INHERITED(s.fChars, s.fLength) {}
|
2017-09-11 20:50:14 +00:00
|
|
|
|
2021-02-18 01:43:54 +00:00
|
|
|
static String printf(const char* fmt, ...) SK_PRINTF_LIKE(1, 2);
|
|
|
|
void appendf(const char* fmt, ...) SK_PRINTF_LIKE(2, 3);
|
2017-03-31 17:56:23 +00:00
|
|
|
void vappendf(const char* fmt, va_list va);
|
|
|
|
|
2020-11-13 20:55:27 +00:00
|
|
|
bool startsWith(const char prefix[]) const {
|
|
|
|
return StringFragment(data(), size()).startsWith(prefix);
|
|
|
|
}
|
|
|
|
bool endsWith(const char suffix[]) const {
|
|
|
|
return StringFragment(data(), size()).endsWith(suffix);
|
|
|
|
}
|
|
|
|
|
2020-09-16 21:43:11 +00:00
|
|
|
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);
|
|
|
|
|
2020-12-23 23:58:43 +00:00
|
|
|
bool stod(const StringFragment& s, SKSL_FLOAT* value);
|
|
|
|
bool stoi(const StringFragment& s, SKSL_INT* value);
|
2017-03-31 17:56:23 +00:00
|
|
|
|
2020-12-23 23:58:43 +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) {
|
2021-03-04 19:30:25 +00:00
|
|
|
result = result * 101 + (size_t) s.fChars[i];
|
2017-09-11 20:50:14 +00:00
|
|
|
}
|
|
|
|
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
|