skia2/include/private/SkSLString.h
John Stiles 33fb008669 Add parser support for u suffix on literals.
Because SkSL is much more permissive than GLSL about literal types, we
don't actually need to treat values any differently when the `u` suffix
is added. That is, `uint x = 4000000000;` already worked fine. When we
encounter the `u`, we just ignore it. This also means that a literal
like `-100u` would be accepted without complaint (although you'd get a
range error if you tried `uint x = -100u;`).

The value-add here is that it removes a speed bump when porting GLSL
code to SkSL. The Filament example shader used the `u` suffix anywhere
that bitwise ops were present; finding and removing all of them was a
chore.

Also of note: the `u` suffix was only added to GLSL in ES3, but we
"support" it everywhere. (We could go out of our way to detect it in
ES2 and flag an error, but that benefits no one.)

Change-Id: I4bf643612c8cf17710e9bad50a0c16f5936bbe88
Bug: skia:12634
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/471756
Commit-Queue: John Stiles <johnstiles@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2021-11-16 13:50:14 +00:00

81 lines
2.1 KiB
C++

/*
* 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
#include "include/core/SkStringView.h"
#include "include/private/SkSLDefines.h"
#include <cstring>
#include <stdarg.h>
#include <string>
#ifndef SKSL_STANDALONE
#include "include/core/SkString.h"
#endif
namespace SkSL {
class String;
class SK_API String : public std::string {
public:
using std::string::string;
explicit String(std::string s) : INHERITED(std::move(s)) {}
explicit String(skstd::string_view s) : INHERITED(s.data(), s.length()) {}
// TODO(johnstiles): add operator skstd::string_view
static String printf(const char* fmt, ...) SK_PRINTF_LIKE(1, 2);
void appendf(const char* fmt, ...) SK_PRINTF_LIKE(2, 3);
void vappendf(const char* fmt, va_list va);
bool starts_with(const char prefix[]) const {
return skstd::string_view(data(), size()).starts_with(prefix);
}
bool ends_with(const char suffix[]) const {
return skstd::string_view(data(), size()).ends_with(suffix);
}
bool consumeSuffix(const char suffix[]);
String operator+(const char* s) const;
String operator+(const String& s) const;
String operator+(skstd::string_view s) const;
String& operator+=(char c);
String& operator+=(const char* s);
String& operator+=(const String& s);
String& operator+=(skstd::string_view s);
friend String operator+(const char* s1, const String& s2);
private:
using INHERITED = std::string;
};
String operator+(skstd::string_view left, skstd::string_view right);
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);
bool stod(skstd::string_view s, SKSL_FLOAT* value);
bool stoi(skstd::string_view s, SKSL_INT* value);
} // namespace SkSL
namespace std {
template<> struct hash<SkSL::String> {
size_t operator()(const SkSL::String& s) const {
return hash<std::string>{}(s);
}
};
} // namespace std
#endif