Major string_view refactor. Added namespace sv
This commit is contained in:
@@ -1,3 +1,18 @@
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// NAMESPACE SV
|
||||
//
|
||||
// * Operate on string_view or just characters.
|
||||
// * Do not allocate memory.
|
||||
// * Do not copy strings.
|
||||
//
|
||||
// NAMESPACE UTIL
|
||||
//
|
||||
// * General purpose utility functions.
|
||||
// * Sort of a catch-all.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef UTIL_HPP
|
||||
#define UTIL_HPP
|
||||
|
||||
@@ -15,6 +30,92 @@
|
||||
#include "luastack.hpp"
|
||||
#include "spookyv2.hpp"
|
||||
|
||||
namespace sv {
|
||||
|
||||
// Bring this into our namespace.
|
||||
using string_view = std::string_view;
|
||||
|
||||
// Test character class, ignoring current locale and unicode issues.
|
||||
inline bool ascii_isupper(char c) { return (c >= 'A') && (c <= 'Z'); }
|
||||
inline bool ascii_islower(char c) { return (c >= 'a') && (c <= 'z'); }
|
||||
inline bool ascii_isdigit(char c) { return (c >= '0') && (c <= '9'); }
|
||||
inline bool ascii_isalpha(char c) { return ascii_isupper(c) || ascii_islower(c); }
|
||||
inline bool ascii_isalnum(char c) { return ascii_isalpha(c) || ascii_isdigit(c); }
|
||||
inline bool ascii_isspace(char c) { return (c==' ')||(c=='\t')||(c=='\r')||(c=='\n')||(c=='\f')||(c=='\v'); }
|
||||
|
||||
// Check for the null string_view
|
||||
//
|
||||
// Note that the null string view is an empty string,
|
||||
// but not every empty string is the null string view.
|
||||
//
|
||||
inline bool isnull(string_view v) { return v.data() == nullptr; }
|
||||
|
||||
// Check if numbers can be parsed as int64/double
|
||||
bool valid_int64(string_view v);
|
||||
bool valid_double(string_view v);
|
||||
|
||||
// Parse numbers as int64/double. Returns errval on failure.
|
||||
int64_t to_int64(string_view v, int64_t errval = std::numeric_limits<int64_t>::min());
|
||||
double to_double(string_view v, double errval = std::numeric_limits<double>::quiet_NaN());
|
||||
|
||||
// Trim whitspace from a string_view.
|
||||
string_view ltrim(string_view v);
|
||||
string_view rtrim(string_view v);
|
||||
string_view trim(string_view v);
|
||||
|
||||
// Trim specific character (all occurrences) from a string_view.
|
||||
string_view ltrim(string_view v, char c);
|
||||
string_view rtrim(string_view v, char c);
|
||||
string_view trim(string_view v, char c);
|
||||
|
||||
// Return true if the string has the specified prefix or suffix.
|
||||
bool has_prefix(string_view s, string_view prefix);
|
||||
bool has_suffix(string_view s, string_view suffix);
|
||||
|
||||
// Return the length of the common prefix of A and B.
|
||||
int common_prefix_length(string_view a, string_view b);
|
||||
|
||||
// Return true if the string is a lua identifier.
|
||||
bool is_lua_id(string_view s);
|
||||
|
||||
// Return true if the line of code is a lua comment.
|
||||
bool is_lua_comment(string_view s);
|
||||
|
||||
// Read from a string_view until separator is reached.
|
||||
//
|
||||
// If the separator appears in the source, returns everything
|
||||
// before the separator, and updates the source to everything
|
||||
// after the separator.
|
||||
//
|
||||
// If the separator doesn't appear in the source, returns
|
||||
// the entire source, and replaces source with the null string_view.
|
||||
//
|
||||
string_view read_to_sep(string_view &source, char sep);
|
||||
|
||||
// Read from a string_view until newline is reached.
|
||||
//
|
||||
// If there's a line-break in the source (newline or CRLF),
|
||||
// returns the text before the line-break, and updates the
|
||||
// source to the text after the line-break.
|
||||
//
|
||||
// If there's no line-break in the source, returns the entire source,
|
||||
// and updates source to the null string_view.
|
||||
//
|
||||
string_view read_to_line(string_view &source);
|
||||
|
||||
// Read from a string_view until whitespace is reached.
|
||||
//
|
||||
// If there's any whitespace in the source, returns the text
|
||||
// before the whitespace, and update the source to the text
|
||||
// after the whitespace.
|
||||
//
|
||||
// If there's no whitespace in the source, returns the entire
|
||||
// source, and updates the source to the null string_view.
|
||||
//
|
||||
string_view read_to_space(string_view &source);
|
||||
|
||||
} // namespace sv
|
||||
|
||||
namespace util {
|
||||
|
||||
enum WorldType {
|
||||
@@ -38,26 +139,14 @@ using LuaSourceVec = eng::vector<StringPair>;
|
||||
using LuaSourcePtr = std::unique_ptr<LuaSourceVec>;
|
||||
using HashValue = std::pair<uint64_t, uint64_t>;
|
||||
using IdVector = eng::vector<int64_t>;
|
||||
using OptionalInt64 = std::optional<int64_t>;
|
||||
|
||||
// This value is sometimes used to represent 'bad integer'
|
||||
const int64_t BADINT64 = std::numeric_limits<int64_t>::min();
|
||||
|
||||
// Test character class ignoring 'current locale'.
|
||||
inline bool ascii_isupper(char c) { return (c >= 'A') && (c <= 'Z'); }
|
||||
inline bool ascii_islower(char c) { return (c >= 'a') && (c <= 'z'); }
|
||||
inline bool ascii_isdigit(char c) { return (c >= '0') && (c <= '9'); }
|
||||
inline bool ascii_isalpha(char c) { return ascii_isupper(c) || ascii_islower(c); }
|
||||
inline bool ascii_isalnum(char c) { return ascii_isalpha(c) || ascii_isdigit(c); }
|
||||
eng::string ascii_tolower(const eng::string &c);
|
||||
eng::string ascii_toupper(const eng::string &c);
|
||||
// Ascii uppercase and lowercase.
|
||||
eng::string ascii_tolower(std::string_view c);
|
||||
eng::string ascii_toupper(std::string_view c);
|
||||
|
||||
// Return seconds elapsed, for profiling purposes.
|
||||
double profiling_clock();
|
||||
|
||||
// Return true if the string is a valid lua identifier.
|
||||
bool is_identifier(const eng::string &str);
|
||||
|
||||
// Output a string to a stream using Lua string escaping and quoting.
|
||||
void quote_string(const eng::string &str, std::ostream *os);
|
||||
|
||||
@@ -101,50 +190,10 @@ eng::string join(const StringVec &strs, eng::string sep);
|
||||
// Return N repetitions of string A
|
||||
eng::string repeat_string(const eng::string &a, int n);
|
||||
|
||||
// Return the length of the common prefix of A and B.
|
||||
int common_prefix_length(const eng::string &a, const eng::string &b);
|
||||
|
||||
// String to lowercase/uppercase. Ascii only, no unicode.
|
||||
eng::string tolower(eng::string input);
|
||||
eng::string toupper(eng::string input);
|
||||
|
||||
// Return true if the string has the specified prefix or suffix.
|
||||
bool has_prefix(const eng::string &s, const eng::string &prefix);
|
||||
bool has_suffix(const eng::string &s, const eng::string &suffix);
|
||||
|
||||
// Check if numbers can be parsed as int64/double
|
||||
bool valid_int64(std::string_view v);
|
||||
bool valid_double(std::string_view v);
|
||||
|
||||
// Parse numbers as int64/double. Returns errval on failure.
|
||||
int64_t str_to_int64(std::string_view v, int64_t errval = INT64_MIN);
|
||||
double str_to_double(std::string_view v, double errval = std::numeric_limits<double>::quiet_NaN());
|
||||
|
||||
// Trim a string_view
|
||||
std::string_view sv_ltrim(std::string_view v);
|
||||
std::string_view sv_rtrim(std::string_view v);
|
||||
std::string_view sv_trim(std::string_view v);
|
||||
|
||||
std::string_view sv_ltrim(std::string_view v, char c);
|
||||
std::string_view sv_rtrim(std::string_view v, char c);
|
||||
std::string_view sv_trim(std::string_view v, char c);
|
||||
|
||||
// sv_is_null is different from checking for empty.
|
||||
inline bool sv_is_null(const std::string_view &v) { return v.data() == nullptr; }
|
||||
|
||||
// Split a string view into stuff before and after separator.
|
||||
// If the separator doesn't occur, returns a null string view
|
||||
// and doesn't modify the source.
|
||||
std::string_view sv_split_one(std::string_view &source, char sep);
|
||||
|
||||
// Read a line from a string_view.
|
||||
std::string_view sv_read_line(std::string_view &source);
|
||||
|
||||
// Trim strings: left end, right end, both ends.
|
||||
eng::string ltrim(std::string_view s);
|
||||
eng::string rtrim(std::string_view s);
|
||||
eng::string trim(std::string_view s);
|
||||
|
||||
// Calculate distance between two points
|
||||
double distance_squared(double x1, double y1, double x2, double y2);
|
||||
|
||||
@@ -154,9 +203,6 @@ bool world_type_authoritative(util::WorldType wt);
|
||||
// Make a LuaSourceVec with one element, for unit testing.
|
||||
LuaSourcePtr make_lua_source(const eng::string &code);
|
||||
|
||||
// Return true if the line of code is a lua comment.
|
||||
bool is_lua_comment(const eng::string &line);
|
||||
|
||||
// Remove nullptrs from a vector of unique pointers.
|
||||
template<class T>
|
||||
void remove_nullptrs(eng::vector<std::unique_ptr<T>> &vec) {
|
||||
|
||||
Reference in New Issue
Block a user