x65/struse.h

5259 lines
152 KiB
C++

/*
String User Classes
The MIT License (MIT)
Copyright (c) 2015 Carl-Henrik Skårstedt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
https://github.com/sakrac/struse
Add this #define to *one* C++ file before #include "struse.h" to create the implementation:
#define STRUSE_IMPLEMENTATION
// in other words, this sequence should be at the top of one file:
#include ...
#define STRUSE_IMPLEMENTATION
#include "struse.h"
*/
#ifndef __STRUSE_H__
#define __STRUSE_H__
#include <inttypes.h> // uint32_t etc.
#include <string.h> // memcpy, memmove
#include <stdio.h> // printf, vsnprintf
#include <stdarg.h> // va_list
//
// Naming Rules:
//
// find* returns a position of a string where a match if found or -1 if not
// skip*/clip* trims beginning or end of string based on condition
// get_* returns a property, single character or substring
// before*/after* returns a substring matching the condition
// is_* returns a bool for a character or whole string test
// len_* return the number of characters matching the condition from the start of the string
// *_last indicates that search is done from the end of the string
// *_rh indicates a rolling hash search is used
// *_esc indicates that the search string allows for escape codes (\x => x)
// same_str* is a full string compare and returns true or false based on condtions
// *_range indicates that the result is filtered by a given set of valid characters
//
typedef unsigned int strl_t;
// helper defines for sprintf/printf with strop
// example: printf("string is " STROP_FMT "\n", STROP_ARG(strref))
#define STRREF_FMT "%.*s"
#define STRREF_ARG(s) (int)(s).get_len(), (s).get()
// internal helper functions for strref
int _find_rh(const char *text, strl_t len, const char *comp, strl_t comp_len);
int _find_rh_case(const char *text, strl_t len, const char *comp, strl_t comp_len);
// strref holds a reference to a constant substring (const char*)
class strref {
protected:
const char *string;
strl_t length;
public:
strref() { clear(); }
strref(const char *str) { set(str); }
strref(char *str) { set((const char*)str); }
strref(const char *str, strl_t len) : string(str), length(len) {}
strref(const char *str, int len) : string(str), length(strl_t(len)) {}
strref(char *str, int len) : string((const char*)str), length(strl_t(len)) {}
strref(char *str, strl_t len) : string((const char*)str), length(len) {}
bool valid() const { return string && length; }
void clear() { string = nullptr; length = 0; }
void set(const char *str);
const char* get() const { return string; }
const uint8_t* get_u() const { return (const uint8_t*)string; }
strl_t get_len() const { return length; }
char get_first() const { return (string && length) ? *string : 0; }
char get_last() const { return (string && length) ? string[length-1] : 0; }
char pop_first() { if (length && string) { char c = *string++; --length; return c; } return 0; }
strl_t limit_pos(strl_t pos) { return pos<length ? pos : length; }
bool is_substr(const char *sub) const { return sub>=string && sub<=(string+length); }
strl_t substr_offs(strref substr) const {
if (is_substr(substr.get())) return strl_t(substr.get()-get()); return 0; }
strl_t substr_end_offs(strref substr) const {
if (is_substr(substr.get())) return strl_t(substr.get()-get()) + substr.get_len(); return 0; }
bool is_empty() const { return length==0; }
// get fnv1a hash for string
unsigned int fnv1a(unsigned int seed = 2166136261) const;
unsigned int fnv1a_lower(unsigned int seed = 2166136261) const;
unsigned int fnv1a_append(unsigned int base_fnv1a_hash) const { return fnv1a(base_fnv1a_hash); }
unsigned short fnv1a_16( unsigned int seed = 2166136261 ) const;
uint64_t fnv1a_64(uint64_t seed = 14695981039346656037ULL) const;
// whitespace ignore fnv1a (any sequence whitespace is replaced by one space)
unsigned int fnv1a_ws(unsigned int seed = 2166136261) const;
// convert string to basic integer
int64_t atoi() const;
uint64_t atoui() const;
// convert string to floating point
float atof() const;
double atod() const;
// number of characters of basic integer in string
int atoi_skip();
// convert hexadecimal string to signed integer
int ahextoi() const;
// convert hexadecimal string to unsigned integer
size_t ahextoui() const;
uint64_t ahextou64() const;
size_t ahextoui_skip();
size_t abinarytoui_skip();
// output string with newline (printf)
void writeln();
// single digit number
static char num_to_char(uint8_t num) { return num<10 ? (num+'0'):(num+'a'-10); }
// is character empty such as space, tab, linefeed etc.?
static bool is_ws(uint8_t c) { return c <= ' '; }
static bool is_ws(char c) { return (uint8_t)c <= ' '; }
// is character a number?
static bool is_number(uint8_t c) { return c>='0' && c<='9'; }
static bool is_number(char c) { return (uint8_t)c>='0' && (uint8_t)c<='9'; }
// is character a hexadecimal number?
static bool is_hex(uint8_t c) { return is_number(c) || (c>='A' && c<='F') || (c>='a' && c<='f'); }
static bool is_hex(char c) { return is_hex(uint8_t(c)); }
// is character alphabetic (A-Z or a-z)?
static bool is_alphabetic(uint8_t c) { return (c>='a' && c<='z') || (c>='A' && c<='Z'); }
// is character alphabetic or numeric?
static bool is_alphanumeric(uint8_t c) { return is_number(c) || is_alphabetic(c); }
// is character valid as part of a label? (num, _, A-Z, a-z)
static bool is_valid_label(uint8_t c) { return c=='_' || is_alphanumeric(c); }
// word separators are non-alphanumeric characters except apostrophe.
static bool is_sep_ws(uint8_t c) { return c!='\'' && !is_alphanumeric(c); }
static bool is_sep_ws(char c) { return is_sep_ws(uint8_t(c)); }
// is control character? (!-/, ?-@, [-^, {-~)
static bool is_control(uint8_t c) { return !is_ws(c) && !is_alphanumeric(c) && c!='_'; }
// choice of upper/lowercase conversions
static char tolower(char c);
static char toupper(char c);
static char tolower_win(char c);
static char toupper_win(char c);
static char tolower_amiga(char c);
static char toupper_amiga(char c);
static char tolower_macos(char c);
static char toupper_macos(char c);
static size_t tolower_unicode(int c);
static size_t toupper_unicode(int c);
// operators
// strref += int: move string forward (skip)
void operator+=(const strl_t skip) { if (skip<length) { string += skip; length -= skip; } else clear(); }
void operator+=(const int skip) { if (strl_t(skip)<length) { string += skip; length -= strl_t(skip); } else clear(); }
// strref + int: return a string that is moved forward (skip)
strref operator+(const strl_t skip) { if (skip<length) return strref(string+skip, length-skip); return strref(); }
strref operator+(const int skip) { if (skip>=0 && strl_t(skip)<length) return strref(string+skip, length-skip); return strref(); }
// (bool): check if string is valid (0 length strings are invalid)
operator bool() const { return string && length; }
// strref++: skip string forward by one character
strref& operator++() { if (valid()) { string++; length--; } return *this; }
// strref > strref / strref < strref: greater than and lesser than operators
bool operator>(const strref o) const;
bool operator<(const strref o) const;
// strref[int]: get character at position
char operator[](strl_t pos) const { return pos<length ? string[pos] : 0; }
char operator[](int pos) const { return (strl_t)pos<length ? string[pos] : 0; }
// remove from start or end
void skip(strl_t count) { if (count<length) { string += count; length -= count; } else clear(); }
void clip(strl_t count) { if (count<length) { length -= count; } else clear(); }
// check if there are any potential non-text characters in the string
bool valid_ascii7() const;
// fetch a utf-8 character from the beginning of this string
size_t get_utf8() const;
// fetch a utf-8 character from the beginnning of this string and move cursor forward to the next utf-8 character
size_t pop_utf8();
// return character at position
char get_at(strl_t pos) const { return string && pos<length ? string[pos] : 0; }
char get_at(int pos) const { return string && (strl_t)pos<length ? string[pos] : 0; }
// return uint8_tacter at position
uint8_t get_u_at(strl_t pos) const { return (uint8_t)get_at(pos); }
// is there a whitespace at position offs in the string?
bool whitespace_at(strl_t pos) const { return is_ws(get_u_at(pos)); }
// count occurrences of char c
int count_char(char c) const;
// count characters to end of line
strl_t len_eol() const;
// characters start of next line (may be end of string)
strl_t len_next_line() const;
// count characters that can be converted to a floating point number
strl_t len_float_number() const;
// count characters that can be converted to a hexadecimal number
strl_t len_hex() const;
// check if string is a valid floating point number
bool is_float_number() const { return valid() && len_float_number() == length; }
// check if matching first char and skip if match
bool grab_char( char c ) { if(length && string[0]==c) { length--; string++; return true; } return false; }
// wildcard search
strref find_wildcard(const strref wild, strl_t pos = 0, bool case_sensitive = true) const;
strref next_wildcard(const strref wild, strref prev, bool case_sensitive = true) const {
return find_wildcard(wild, is_substr(prev.get()) ? (strl_t(prev.get()-get())+1) : 0, case_sensitive); }
strref wildcard_after(const strref wild, strref prev, bool case_sensitive = true) const {
return find_wildcard(wild, is_substr(prev.get()) ? strl_t(prev.get()+prev.get_len()-get()) : 0, case_sensitive); }
// character filter by string, as in a wildcard [] operator
bool char_matches_ranges(uint8_t c) const;
bool char_matches_ranges(char c) const { return char_matches_ranges((uint8_t)c); }
// whole string compare (_case indicates case sensitive)
bool same_str(const strref str) const;
bool same_str_case(const strref str) const;
bool same_str(const char *str) const;
bool same_str_case(const char *str);
// whole string compare, treat character same1 and same2 as equal
bool same_str(const strref str, char same1, char same2) const;
bool same_str_case(const strref str, char same1, char same2) const;
// mid string compare
bool same_substr(const strref str, strl_t pos) const;
bool same_substr_esc(const strref str, strl_t pos) const;
bool same_substr_case(const strref str, strl_t pos) const;
bool same_substr_case_esc(const strref str, strl_t pos) const;
bool same_substr_case_esc(const strref str, int pos) const { return same_substr_case_esc(str, (strl_t)pos); }
bool same_substr_case_esc_range(const strref str, const strref range, strl_t pos) const;
// prefix compare
strl_t prefix_len(const strref str) const;
strl_t prefix_len_case(const strref str) const;
strl_t prefix_len(const char *str) const;
strl_t prefix_len_case(const char *str) const;
strl_t prefix_len(const strref str, char same1, char same2) const;
bool has_prefix(const strref str) const { return prefix_len(str) == str.get_len(); }
bool has_prefix(const char* str) const { return str[prefix_len(str)]==0; }
bool is_prefix_of(const strref str) const { return prefix_len(str)==get_len(); }
bool is_prefix_of(const strref str, char same1, char same2) const {
return prefix_len(str, same1, same2)==get_len(); }
bool is_prefix_word(const strref str) const { return prefix_len(str)==get_len() &&
(str.get_len()==length || !is_alphanumeric((uint8_t)str[length])); }
bool is_prefix_case_of(const strref str) const { return prefix_len_case(str)==get_len(); }
bool is_prefix_float_number() const { return len_float_number() > 0; }
bool grab_prefix( const char* str ) { strl_t p = prefix_len( str ); if( !str[ p ] ) { skip( p ); return true; } return false; }
// suffix compare
strl_t suffix_len(const strref str) const;
strl_t suffix_len_case(const strref str) const;
bool is_suffix_of(const strref str) const { return suffix_len(str)==get_len(); }
bool is_suffix_case_of(const strref str) { return suffix_len_case(str)==get_len(); }
bool has_suffix(const char *str) const { return strref(str).is_suffix_of(*this); }
// whole word compare (prefix match + next char is whitespace or end of string)
bool is_word(const strref str) const { return prefix_len(str)==str.get_len() && whitespace_at(str.get_len()); }
bool is_word_case(const strref str) const { return prefix_len_case(str)==str.get_len() && whitespace_at(str.get_len()); }
// string search
// find first index of char c
int find(char c) const;
// find first index of char c after pos
int find_at(char c, strl_t pos) const;
// find first index of char c after index pos (find_at(c, pos+1))
int find_after(char c, strl_t pos) const;
// find first index of char c after index pos or return length for full string
strl_t find_or_full(char c, strl_t pos) const;
strl_t find_or_full_esc(char c, strl_t pos) const;
// find last position of char c
int find_last(char c) const;
// find first position of either char c or char d
int find(char c, char d) const;
// find last position of either char c or char d
int find_last(char c, char d) const;
// find first <b> after last <a> in string
int find_after_last(char a, char b) const { return find_after(b, (strl_t)(find_last(a)+1)); }
int find_after_last(char a1, char a2, char b) const {
int w = find_last(a1, a2)+1; int l = strref(string+w, length-w).find(b); return l>=0?l+w:-1; }
// return position in this string of the first occurrence of the argument or negative if not found, not case sensitive
int find(const strref str) const;
int find(const strref str, strl_t pos) const; // find first instance after pos
int find_bookend(const strref str, const strref bookend) const;
// return position in this string of the first occurrence of the argument or negative if not found, not case sensitive
int find(const char *str, strl_t pos = 0) const;
strl_t find_or_full(const char *str) const { int f = find(str); return f < 0 ? length : strl_t(f); }
// return position in this string of the first occurrence of the argument or negative if not found, case sensitive
int find_case(const strref str, strl_t pos = 0) const;
// return position in this string of the first occurrence of the argument or negative if not found, case sensitive
int find_case(const char *str) const;
int find_case_esc(const strref str, strl_t pos) const;
int find_case_esc_range(const strref str, const strref range, strl_t pos) const;
int find_esc_range(const strref str, const strref range, strl_t pos) const;
// return position in this string of the last occurrence of the argument or negative if not found, not case sensitive
int find_last(const strref str) const;
int find_last_bookend(const strref str, const strref bookend) const;
// return position in this string of the last occurrence of the argument or negative if not found, not case sensitive
int find_last(const char *str) const;
// return position in this string of the last occurrence of the argument or negative if not found, case sensitive
int find_last_case(const strref str) const;
// find first instance after pos allowing escape codes in search string
int find_esc(const strref str, strl_t pos) const;
// find any char from str in this string at position
int find_any_char_of(const strref range, strl_t pos = 0) const;
// find any char from str or char range or char - with backslash prefix
int find_any_char_or_range(const strref range, strl_t pos = 0) const;
int find_any_not_in_range(const strref range, strl_t pos = 0) const;
// find any char from str or char range or char - with backslash prefix
int find_range_char_within_range(const strref range_find, const strref range_within, strl_t pos = 0) const;
// find but not within parenthesis
int find_skip_parens(char token) const;
// counts
int substr_count(const strref str) const; // count the occurrences of the argument in this string
int substr_count_bookend(const strref str, const strref bookend) const;
int substr_case_count(const strref str) const; // count the occurrences of the argument in this string
int substr_label_case_count(const strref str) const;
int count_repeat(char c, strl_t pos) const;
int count_repeat_reverse(char c, strl_t pos) const;
int count_lines() const;
int count_lines(strl_t pos) const { return strref(string, pos<length ? pos : length).count_lines(); }
int count_lines(strref substring) const { return is_substr(substring.get()) ?
count_lines(strl_t(substring.get()-get())) : -1; }
strl_t prev_line_pos( strl_t pos );
strl_t start_line_pos( strl_t pos );
strl_t end_line_pos( strl_t pos );
// rolling hash find
int find_rh(strref str) const { return _find_rh(get(), get_len(), str.get(), str.get_len()); }
int find_rh_case(strref str) const {
return _find_rh_case(get(), get_len(), str.get(), str.get_len()); }
int find_rh(strref str, strl_t pos) const {
strref sub = *this; sub += pos; return sub.find_rh(str); }
int find_rh_case(strref str, strl_t pos) const {
strref sub = *this; sub += pos; return sub.find_rh_case(str); }
int find_rh_after(strref str, strref prev) const {
strref sub = *this; if (is_substr(prev.get())) sub.skip(strl_t(prev.get()-get()));
return sub.find_rh(str); }
int find_rh_case_after(strref str, strref prev) const {
strref sub = *this; if (is_substr(prev.get())) sub.skip(strl_t(prev.get()-get()));
return sub.find_rh_case(str); }
// whitespace management
// number of white space characters from start of string
strl_t len_whitespace() const { if (!valid()) return 0;
strl_t o = 0; while (o<length && is_ws((uint8_t)string[o])) o++; return o; }
// number of white space characters from pos
strl_t len_whitespace(strl_t pos) const {
strl_t o = pos; while (o<length && is_ws((uint8_t)string[o])) o++; return o-pos; }
// number of separator or white space characters from pos
strl_t len_sep_ws(strl_t pos) const {
strl_t o = pos; while (o<length && is_sep_ws((uint8_t)string[o])) o++; return o-pos; }
strl_t len_sep_ws(int pos) const { return len_sep_ws((strl_t)pos); }
// number of non white space characters from start of string
strl_t len_grayspace() const { if (!valid()) return 0;
strl_t o = 0; while (o<length && !is_ws((uint8_t)string[o])) o++; return o; }
// number of non white space characters from pos
strl_t len_grayspace(strl_t pos) const {
strl_t o = pos; while (o<length && !is_ws((uint8_t)string[o])) o++; return o-pos; }
// number of non separating non white space characters from pos
strl_t len_non_sep_ws(strl_t pos) const {
strl_t o = pos; while (o<length && !is_sep_ws((uint8_t)string[o])) o++; return o-pos; }
strl_t len_non_sep_ws(int pos) const { return len_non_sep_ws((strl_t)pos); }
// move string forward to the first non-whitespace character
void skip_whitespace() { skip(len_whitespace()); }
// move string forward to the first whitespace character
void skip_to_whitespace() { skip(len_grayspace()); }
// if bom detected skip it
strref skip_bom();
// cut white space characters at end of string
void clip_trailing_whitespace() { if (valid()) {
const char *e = string+length; while (*--e<=0x20 && length) { length--; } } }
// remove white space from start and end
void trim_whitespace() { clip_trailing_whitespace(); skip_whitespace(); }
strref get_trimmed_ws() const { strref ret = *this;
ret.clip_trailing_whitespace(); ret.skip_whitespace(); return ret; }
// find next whitespace or entire string
strl_t find_whitespace_or_full() const {
strl_t ws = (strl_t)len_grayspace(); return (ws==length) ? 0 : ws; }
// find next whitespace or -1
int find_whitespace() const {
strl_t ws = len_grayspace(); return (ws==length) ? -1 : (int)ws; }
// string content
// count number of alphanumeric characters from p
strl_t len_alphanumeric(strl_t p = 0) const { if (valid()) {
const char *s = string+p; strl_t r = length-p; while (is_alphanumeric((uint8_t)*s) && r) { s++; r--; }
return length-r; } return 0; }
// length of word consisting of alphanumeric characters
strl_t len_word() const { return len_alphanumeric(0); }
// length of string with escape characters
strl_t len_esc() const;
// return number of characters that can be a label
strl_t len_label() const { if (valid()) { const uint8_t *s = get_u(); strl_t r = length;
while (is_valid_label(*s) && r) { s++; r--; } return length-r; } return 0; }
// return whether or not this string is a number
bool is_number() const { if (strl_t r = length) { if (const uint8_t *s = get_u()) {
while (r) { if (!is_number(*s)) return false; s++; r--; } return true; } } return false; }
// go to the next word
void next_word_ws() { skip_whitespace(); skip_to_whitespace(); skip_whitespace(); }
// go to the end of this word
void end_word() { strl_t a = len_word(); string += a; length -= a; }
// get a portion of a string
strref get_substr(strl_t pos, strl_t len) const { return pos<length ?
(strref(string+pos, (pos+len)<length?len : (length-pos))) : strref(); }
strref get_substr(int pos, int len) const { return get_substr((strl_t)pos, (strl_t)len); }
strref get_substr(int pos, strl_t len) const { return get_substr((strl_t)pos, len); }
strref get_skipped(strl_t len) const { if (len<length)
return strref(string+len, length-len); return strref(); }
// get this strref without leading whitespace
strref get_skip_ws() const { return get_skipped(len_whitespace()); }
strref get_clipped(strl_t len) const { return strref(len>0?string:nullptr,
len>0?(strl_t(len)<length?len:length):0); }
strref get_word() const { return get_clipped(len_word()); }
// get a range of characters matching the range
strref get_range_word(const strref range, strl_t pos = 0) const;
// get the next block of characters separated by whitespace
strref get_word_ws() const {
strl_t w = len_whitespace(), g = len_grayspace(w); return get_substr(w, g - w); }
strref get_valid_json_string() const {
const uint8_t *s = get_u(); strl_t l = length; while (l) {
uint8_t c = *s++; if (!(c=='+' || c=='.' || c=='-' || is_number(c) || c>='A'))
break; l--; } return strref(string, length-l); }
strref before(char c) const {
int o = find(c); if (o>=0) return strref(string, o); return strref(); }
strref before(char c, char d) const {
int o = find(c, d); if (o>=0) return strref(string, o); return strref(); }
strref before_or_full(char c) const {
int o = find(c); if (o>=0) return strref(string, o); return *this; }
strref before_or_full_track_parens(char c) const {
int o = find_skip_parens(c); if (o >= 0) return strref(string, o); return *this;
}
strref before_last(char c) const {
int o = find_last(c); if (o>=0) return strref(string, o); return strref(); }
strref before_last(char c, char d) const {
int o = find_last(c, d); if (o>=0) return strref(string, o); return strref(); }
strref before_or_full(const strref str) const {
int o = find(str); if (o<0) return *this; return strref(string, o); }
strref after_or_full(const strref str) const {
int o = find(str); if (o<0) return *this; return strref(string+o, length-o); }
strref after_or_full(char c) const { int o = find(c);
if (o>=0) return strref(string+o+1, length-o-1); return *this; }
strref after_or_full(char c, char d) const { int o = find(c, d);
if (o>=0) return strref(string+o+1, length-o-1); return *this; }
strref after(char c) const { int o = find(c);
if (o>=0) return strref(string+o+1, length-o-1); return strref(); }
strref after_last_or_full(char c) const { int o = find_last(c);
if (o>=0) return strref(string+o+1, length-o-1); return *this; }
strref after_last_or_full(char c, char d) const {
int o = find_last(c, d); if (o>=0) return strref(string+o+1, length-o-1); return *this; }
strref after_last(char c) const { int o = find_last(c); if (o>=0)
return strref(string+o+1, length-o-1); return strref(); }
strref after_last(char c, char d) const { int o = find_last(c, d); if (o>=0)
return strref(string+o+1, length-o-1); return strref(); }
strref get_alphanumeric() const { strref r(*this); r.skip_whitespace();
if (strl_t l = r.len_alphanumeric()) return strref(string, l); return strref(); }
strref get_label() const { return strref(string, len_label()); }
strref before_or_full_case(const strref str) const { int o = find_case(str);
if (o<0) return *this; return strref(string, o); }
strref after_or_full_case(const strref str) const { int o = find_case(str);
if (o<0) return *this; return strref(string+o, length-o); }
strref between(char c, char d) { int s = find(c); if (s>=0) { int e = find_after(d, (strl_t)s);
if (e>=0) return get_substr(strl_t(s+1), strl_t(e-s-1)); } return strref(); }
// tokenization
strref split(strl_t pos);
strref split(int pos);
strref split_token(char c);
strref split_token_any(const strref chars);
strref split_token_trim(char c);
strref split_token_any_trim(const strref chars);
strref split_token_track_parens(char c);
strref split_token_track_parens_quote(char c);
strref split_token_trim_track_parens(char c);
strref split_range(const strref range, strl_t pos=0);
strref split_range_trim(const strref range, strl_t pos=0);
strref split_label();
strref split_lang();
strref split_num();
// get a snippet, previous and full current line around a position
strref get_snippet( strl_t pos );
// grab a block of text starting with (, [ or { and end with the corresponding number of ), ] or }
strref scoped_block_skip( bool quotes = false );
// scoped_block_skip with C style comments
strl_t scoped_block_comment_len();
strl_t scoped_block_utf8_comment_len();
strref scoped_block_comment_skip(bool include = false) { strref ret = split(scoped_block_comment_len()); if (!include) { ++ret; ret.clip(1); } return ret; }
strref scoped_block_utf8_comment_skip( bool include = false ) {
strref ret = split( scoped_block_utf8_comment_len() );
if( !include ) { ++ret; ret.clip( 1 ); }
return ret;
}
// check matching characters that are terminated by any character in term or ends
strl_t match_chars_str(const strref match, const strref term = strref());
strref get_line() const; // return the current line even if empty,t don't change this line
strref get_line(strl_t line) const; // return line by index
strref next_line(); // return the current line even if empty and skip this to line after
strref line() { strref ret; while (valid() && !ret.valid()) ret = next_line(); return ret; } // return the current or next valid line skip this to line after
strref next_token(char c) { int o = find(c); if (o<0) o = (int)get_len(); return split(o); }
strref token_chunk(char c) const { int o = find(c); if (o<0) return *this; return strref(string, o); }
void token_skip(const strref chunk) { skip(chunk.length+1); }
strref find_token(const char *substr, char token) const;
strref find_token_case(const char *substr, char token) const;
strref find_token(strref substr, char token) const;
strref find_token_case(strref substr, char token) const;
strref within_last(char a, char b) const { int f = find_last(a)+1;
int l = strref(string+f, length-f).find(b); if (l<0) l = 0; return strref(string+f, l); }
strref within_last(char a1, char a2, char b) const { int f = find_last(a1, a2)+1;
int l = strref(string+f, length-f).find(b); if (l<0) l = 0; return strref(string+f, l); }
strref get_quote_xml() const;
strref skip_quote_xml();
int find_quoted_xml(char d) const; // returns length up to the delimiter d with xml quotation rules, or -1 if delimiter not found
int find_quoted(char d) const; // returns length up to the delimiter d with c/c++ quotation rules, or -1 if delimiter not found
strref next_chunk_xml(char open, char close) const { int s = find_quoted_xml(open);
if (s<0) return strref(); strref left = get_skipped(strl_t(s+1)); return left.get_clipped(strl_t(left.find_quoted_xml(close))); }
strref next_chunk_quoted(char open, char close) const { int s = find_quoted(open);
if (s<0) return strref(); strref left = get_skipped(strl_t(s+1)); return left.get_clipped(strl_t(left.find_quoted(close))); }
void skip_chunk(const strref chunk) { strl_t add = strl_t(chunk.string-string)+chunk.length+1UL;
if (add<length) { string += add; length -= add; } else { clear(); } }
};
// internal helper functions for strmod
strl_t _strmod_copy(char *string, strl_t cap, const char *str);
strl_t _strmod_copy(char *string, strl_t cap, strref str);
strl_t _strmod_append(char *string, strl_t length, strl_t cap, const char *str);
strl_t _strmod_append(char *string, strl_t length, strl_t cap, strref str);
strl_t _strmod_insert(char *string, strl_t length, strl_t cap, const strref sub, strl_t pos);
strl_t _strmod_utf8_tolower(char *string, strl_t length, strl_t cap);
strl_t _strmod_write_utf8( char *string, strl_t cap, size_t code, strl_t pos );
void _strmod_substrcopy(char *string, strl_t length, strl_t cap, strl_t src, strl_t dst, strl_t chars);
void _strmod_tolower(char *string, strl_t length);
void _strmod_toupper(char *string, strl_t length);
strl_t _strmod_format_insert(char *string, strl_t length, strl_t cap, strl_t pos, strref format, const strref *args);
strl_t _strmod_append_num(char* str, strl_t left, uint32_t num, strl_t size, uint32_t radix);
strl_t _strmod_remove(char *string, strl_t length, char a);
strl_t _strmod_remove(char *string, strl_t length, strl_t start, strl_t len);
strl_t _strmod_exchange(char *string, strl_t length, strl_t cap, strl_t start, strl_t size, const strref insert);
strl_t _strmod_cleanup_path(char *file, strl_t len);
strl_t _strmod_relative_path(char *out, strl_t cap, strref orig, strref target);
// intermediate template class to support writeable string classes. use strown or strovl which inherits from this.
template <class B> class strmod : public B {
// mirror base class unsafe size operations (doesn't check capacity)
void add_len_int(strl_t l) { B::add_len_int(l); }
void sub_len_int(strl_t l) { B::sub_len_int(l); }
void set_len_int(strl_t l) { B::set_len_int(l); }
void dec_len_int() { B::dec_len_int(); }
void inc_len_int() { B::inc_len_int(); }
public:
strmod() { clear(); }
operator strref() { return strref(charstr(), len()); }
// mirror base template class
strl_t cap() const { return B::cap(); }
char* charstr() { return B::charstr(); }
const char* charstr() const { return B::charstr(); }
strl_t len() const { return B::len(); }
// get a strref version of this string
strref get_strref() { return strref(charstr(), len()); }
strref get_strref() const { return strref(charstr(), len()); }
strl_t get_len() const { return B::len(); }
// basic tests and operations
void clear() { set_len_int(0); }
bool valid() const { return charstr() && len(); }
operator bool() const { return valid(); }
bool empty() const { return !len(); }
bool full() const { return len() == cap(); }
const char* get() const { return charstr(); }
char get_first() const { return (charstr() && len()) ? *charstr() : 0; }
char get_last() const { return (charstr() && len()) ? charstr()[len()-1] : 0; }
void copy(strref o) { set_len_int(_strmod_copy(charstr(), cap(), o)); }
bool is_substr(const char *sub) const { return sub>=charstr() && sub<=(charstr()+len()); }
// public size operators (checks for capacity)
strl_t fit_add(strl_t desired) { return (desired+len()) < cap() ? desired : (cap()-len()); }
bool set_len(strl_t l) { if (l<=cap()) { set_len_int(l); return true; } set_len_int(cap()); return false; }
void add_len(strl_t l) { add_len_int(fit_add(l)); }
// offset operators will always return a strref
strref operator+(const strl_t skip) { if (skip<len())
return strref(charstr()+skip, len()-skip); return strref(); }
strref operator+(const int skip) { if (skip>=0 && strl_t(skip)<len())
return strref(charstr()+skip, len()-skip); return strref(); }
// get character at position
char operator[](size_t pos) { return pos<len() ? charstr()[pos] : 0; }
// mirror strref functionality
int count_char(char c) const { return get_strref().count_char(c); }
int len_eol() const { return get_strref().len_eol(); }
int len_next_line() const { return get_strref().len_next_line(); }
strl_t len_float_number() const { return get_strref().len_float_number(); }
bool is_float_number() const { return get_strref().is_float_number(); }
// get fnv1a hash for string
unsigned int fnv1a(unsigned int seed = 2166136261) const { return get_strref().fnv1a(seed); }
unsigned int fnv1a_append(unsigned int base_fnv1a_hash) const { return get_strref().fnv1a(base_fnv1a_hash); }
// whole string compare
bool same_str(const strref str) const { return get_strref().same_str_case(str); }
bool same_str_case(const strref str) const { return get_strref().same_str_case(str); }
bool same_str(const strref str, char same1, char same2) const {
return get_strref().same_str_case(str, same1, same2); }
bool same_str_case(const strref str, char same1, char same2) const {
return get_strref().same_str_case(str, same1, same2); }
bool same_str(const char *str) const { return get_strref().same_str(str); }
bool same_str_case(const char *str) const { return get_strref().same_str_case(str); }
// prefix compare
strl_t prefix_len(const strref str) const { return get_strref().prefix_len(str); }
strl_t prefix_len(const strref str, char same1, char same2) const {
return get_strref().prefix_len_case(str, same1, same2); }
strl_t prefix_len(const char *str) const { return get_strref().prefix_len(str); }
strl_t prefix_len_case(const strref str) const { return get_strref().prefix_len_case(str); }
strl_t prefix_len_case(const char *str) const { return get_strref().prefix_len_case(str); }
bool is_prefix_of(const strref str) const { return get_strref().is_prefix_of(str); }
bool is_prefix_of(const strref str, char same1, char same2) const {
return get_strref().is_prefix_of(str, same1, same2); }
bool is_prefix_word(const strref str) const { return get_strref().is_prefix_word(str); }
bool is_prefix_case_of(const strref str) const { return get_strref().is_prefix_case_of(str); }
bool is_prefix_float_number() const { return get_strref().is_prefix_float_number(); }
bool grab_prefix( const char* str ) { return get_strref().grab_prefix( str ); }
// whole word compare (prefix match + next char is whitespace or end of string)
bool is_word(const strref str) const { return get_strref().is_word(str); }
bool is_word_case(const strref str) const { return get_strref().is_word_case(str); }
// suffix compare
strl_t suffix_len(const strref str) const { return get_strref().suffix_len(str); }
strl_t suffix_len_case(const strref str) const { return get_strref().suffix_len_case(str); }
bool is_suffix_of(const strref str) const { return get_strref().is_suffix_of(str); }
bool is_suffix_case_of(const strref str) const { return get_strref().is_suffix_case_of(str); }
bool has_suffix(const char *str) const { return get_strref().has_suffix(str); }
// string search
int find(char c) const { return get_strref().find(c); }
int find_after(char c, strl_t pos) const { return get_strref().find_after(c, pos); }
int find_or_full(char c, strl_t pos) const { return get_strref().find_or_full(c, pos); }
int find_last(char c) const { return get_strref().find_last(c); }
int find(char c, char d) const { return get_strref().find(c, d); }
int find_last(char c, char d) const { return get_strref().find_last(c, d); }
int find_after_last(char a, char b) const { return get_strref().find_after_last(a, b); }
int find_after_last(char a1, char a2, char b) const { return get_strref().find_after_last(a1, a2, b); }
int find(const strref str) const { return get_strref().find(str); }
int find(const strref str, strl_t pos) const { return get_strref().find(str, pos); }
int find(const char *str, strl_t pos = 0) const { return get_strref().find(str, pos); }
int find_case(const strref str) const { return get_strref().find_case(str); }
int find_case(const char *str) const { return get_strref().find_case(str); }
int find_last(const strref str) const { return get_strref().find_last(str); }
int find_last(const char *str) const { return get_strref().find_last(str); }
int find_last_case(const strref str) const { return get_strref().find_last_case(str); }
int substr_count(const strref p) const { return get_strref().substr_count(p); }
// rolling hash string search
int find_rh(strref str) const { return get_strref().find_rh(str); }
int find_rh_case(strref str) const { return get_strref().find_rh_case(str); }
int find_rh(strref str, strl_t pos) const { return get_strref().find_rh(str, pos); }
int find_rh_case(strref str, strl_t pos) const { return get_strref().find_rh_case(str, pos); }
int find_rh_after(strref str, strref prev) const { return get_strref().find_rh_after(str, prev); }
int find_rh_case_after(strref str, strref prev) const { return get_strref().find_rh_case_after(str, prev); }
// whitespace management
int len_whitespace() const { return get_strref().len_whitespace(); }
int len_grayspace() const { return get_strref().len_grayspace(); }
strl_t find_whitespace_or_full() const { return get_strref().find_whitespace_or_full(); }
int find_whitespace() const { return get_strref().find_whitespace(); }
// string content
strl_t len_alphanumeric(strl_t p = 0) const { return get_strref().len_alphanumeric(p); }
strl_t len_word() const { return get_strref().len_word(); }
int len_label() const { return get_strref().len_label(); }
bool is_number() const { return get_strref().is_number(); }
// get a portion of a string
strref get_substr(strl_t pos, strl_t length) const { return get_strref().get_substr(pos, length); }
strref get_skipped(int skip_len) const { return get_strref().get_skipped(skip_len); }
strref get_clipped(int clip_len) const { return get_strref().get_clipped(clip_len); }
strref get_word() const { return get_strref().get_word(); }
strref get_json() const { return get_strref().get_json(); }
strref before(char c) const { return get_strref().before(c); }
strref before(char c, char d) const { return get_strref().before(c, d); }
strref before_last(char c) const { return get_strref().before_last(c); }
strref before_last(char c, char d) const { return get_strref().before_last(c, d); }
strref before_or_full(char c) const { return get_strref().before_or_full(c); }
strref before_or_full(const strref str) const { return get_strref().before_or_full(str); }
strref after_or_full(const strref str) const { return get_strref().after_or_full(str); }
strref after_or_full(char c) const { return get_strref().after_or_full(c); }
strref after(char c) const { return get_strref().after(c); }
strref after_last_or_full(char c) const { return get_strref().after_last_or_full(c); }
strref after_last(char c) const { return get_strref().after_last(c); }
strref get_alphanumeric() const { return get_strref().get_alphanumeric(); }
strref before_or_full_case(const strref str) const { return get_strref().before_or_full_case(str); }
strref after_or_full_case(const strref str) const { return get_strref().after_or_full_case(str); }
strref between(char c, char d) const { return get_strref().between(c, d); }
// tokenization
strref line(); // return the current line and skip this to next line
strref token_chunk(char c) const { return get_strref().token_chunk(c); }
strref find_token(const char *substr, char token) const { return get_strref().find_token(substr, token); }
strref find_token_case(const char *substr, char token) const { return get_strref().find_token_case(substr, token); }
strref find_token(strref substr, char token) const { return get_strref().find_token(substr, token); }
strref find_token_case(strref substr, char token) const { return get_strref().find_token_case(substr, token); }
strref within_last(char a, char b) const { return get_strref().within_last(a, b); }
strref within_last(char a1, char a2, char b) const { return get_strref().within_last(a1, a2, b); }
strref get_quote_xml() const { return get_strref().get_quote_xml(); }
int find_quoted_xml(char d) const { return get_strref().find_quoted_xml(d); }
int find_quoted(char d) const { return get_strref().find_quoted(d); }
// wildcard search
strref find_wildcard(const strref wild, strl_t pos = 0, bool case_sensitive = true) const {
return get_strref().find_wildcard(wild, pos, case_sensitive); }
// find a wildcard right after the start of the previous find
strref next_wildcard(const strref wild, strref prev, bool case_sensitive = true) const {
return get_strref().next_wildcard(wild, prev, case_sensitive); }
// find a wildcard right after the end of the previous find
strref wildcard_after(const strref wild, strref prev, bool case_sensitive = true) const {
return get_strref().next_wildcard(wild, prev, case_sensitive); }
// write a single utf-8 character to pos and return how many bytes was required
strl_t write_utf8(int code, strl_t pos) { return _strmod_write_utf8(charstr(), cap(), code, pos); }
// push a single utf-8 character to the end of this string
void push_utf8(int code) { add_len_int(write_utf8(code, len())); }
// get a single utf-8 character
int get_utf8(strl_t pos) { strl_t skip; return _strmod_read_utf8(charstr(), len(), pos, skip); }
// get a single utf-8 characer and the number of bytes that was used to store it
int get_utf8(strl_t pos, strl_t &skip) { return _strmod_read_utf8(charstr(), len(), pos, skip); }
// choice of uppercasing / lowercasing entire string
void tolower() { _strmod_tolower(charstr(), len()); }
void toupper() { _strmod_toupper(charstr(), len()); }
void tolower_win() { _strmod_tolower_win_ascii(charstr(), len()); }
void toupper_win() { _strmod_toupper_win_ascii(charstr(), len()); }
void tolower_amiga() { _strmod_tolower_amiga_ascii(charstr(), len()); }
void toupper_amiga() { _strmod_toupper_amiga_ascii(charstr(), len()); }
void tolower_macos() { _strmod_tolower_macos_ascii(charstr(), len()); }
void toupper_macos() { _strmod_toupper_macos_ascii(charstr(), len()); }
void tolower_utf8() { set_len_int(_strmod_utf8_tolower(charstr(), len(), cap())); }
void toupper_utf8() { set_len_int(_strmod_utf8_toupper(charstr(), len(), cap())); }
// get the end of the current string
char *end() { return charstr()+len(); }
// number of characters left until at capacity
strl_t left() const { return cap()-len(); }
// remove trailing whitespace
void clip_trailing_whitespace() { if (valid()) {
const char *e = end(); while (*--e<=0x20 && len()) { dec_len_int(); } } }
// copy a substring internally while checking for overlap
void substrcopy(strl_t pos, strl_t target, strl_t _length) {
_strmod_substrcopy(charstr(), len(), cap(), pos, target, _length); }
// insert a substring and expand the string to fit it
bool insert(const strref sub, strl_t pos) {
return set_len(_strmod_insert(charstr(), len(), cap(), sub, pos)); }
// append a substring at the end of this string
strmod& append(const strref o) { if (o) { strl_t a = fit_add(o.get_len());
if (a) { memcpy(end(), o.get(), a); add_len_int(a); } } return *this; }
// append a character at the end of this string
strmod& append(char c) { if (!full()) { charstr()[len()] = c; inc_len_int(); } return *this; }
// C#-ish string concatenation
strmod& operator+(char c) { return append(c); }
strmod& operator+(const char* str) { return append(str); }
strmod& operator+(strref str) { return append(str); }
// C++COut-ish string concatenation
strmod& operator<<(char c) { return append(c); }
strmod& operator<<(const char* str) { return append(str); }
strmod& operator<<(strref str) { return append(str); }
// append a character repeatedly
strmod& pad_to(char c, strl_t pos) { if (len() >= pos) set_len_int(pos); else {
strl_t ol = len(); set_len(pos); for (strl_t p = ol; p < len(); ++p) charstr()[p] = c; } return *this; }
// prepend this string with a substring
void prepend(const strref o) { insert(o, 0); }
// prepend this string with a c string
void prepend(const char *s) { insert(strref(s), 0); }
// format this string using {n} notation to index into the args list
void format(const strref format, const strref *args) {
set_len_int(_strmod_format_insert(charstr(), 0, cap(), 0, format, args)); }
// append a formatted string, return the appended part as a strref
strref format_append(const strref format, const strref *args) { strl_t l = len();
set_len_int(_strmod_format_insert(charstr(), len(), cap(), len(), format, args));
return strref(charstr()+l, len()-l); }
// prepend a formatted string, return the prepend part as a strref
strref format_prepend(const strref format, const strref *args) { strl_t l = len();
set_len_int(_strmod_format_insert(charstr(), len(), cap(), 0, format, args));
return strref(charstr(), len()-l); }
// insert a formatted string
void format_insert(const strref format, const strref *args, strl_t pos) {
set_len_int(_strmod_format_insert(charstr(), len(), cap(), pos, format, args)); }
strmod& append_num(uint32_t num, strl_t size, strl_t radix) {
add_len_int( _strmod_append_num( charstr() + len(), cap() - len(), num, size, radix ) );
return *this;
}
// c style sprintf (work around windows _s preference)
#ifdef _WIN32
int sprintf(const char *format, ...) { va_list args; va_start(args, format);
set_len_int((strl_t)vsnprintf_s(charstr(), cap(), _TRUNCATE, format, args)); va_end(args); return (int)len(); }
int sprintf_at(strl_t pos, const char *format, ...) { va_list args; va_start(args, format);
int l = vsnprintf_s(charstr()+pos, cap()-pos, _TRUNCATE, format, args);
if (l+pos>len()) set_len(l+pos); va_end(args); return l; }
int sprintf_append(const char *format, ...) { va_list args; va_start(args, format); strl_t l = 0;
if (len()<cap()) { l = (strl_t)vsnprintf_s(end(), cap()-len(), _TRUNCATE, format, args); va_end(args);
add_len_int(l); } return (int)l; }
#else
int sprintf(const char *format, ...) { va_list args; va_start(args, format);
set_len_int(vsnprintf(charstr(), cap(), format, args)); va_end(args); return len(); }
int sprintf_at(strl_t pos, const char *format, ...) { va_list args; va_start(args, format);
int l = vsnprintf(charstr()+pos, cap()-pos, format, args);
if (l+pos>len()) set_len(l+pos); va_end(args); return l; }
int sprintf_append(const char *format, ...) { va_list args; va_start(args, format);
int l = vsnprintf(end(), cap()-len(), format, args); va_end(args); add_len_int(l); return l; }
#endif
// replace instances of character c with character d
strref replace(char c, char d) { if (char *b = charstr()) {
for (strl_t i = len(); i; i--) { if (*b==c) *b = d; b++; } } return get_strref(); }
// replace instances of substring a with substring b
strref replace(const strref a, const strref b) {
set_len(_strmod_inplace_replace_int(charstr(), len(), cap(), a, b)); return get_strref(); }
// replace strings bookended by a specific string
strref replace_bookend(const strref a, const strref b, const strref bookend) { if (len() && get() && a && bookend)
set_len(_strmod_inplace_replace_bookend_int(charstr(), len(), cap(), a, b, bookend)); return get_strref(); }
// replace a string found within this string with another string
void exchange(strl_t pos, strl_t size, const strref insert) {
set_len_int(_strmod_exchange(charstr(), len(), cap(), pos, size, insert)); }
void exchange(const strref original, const strref insert) {
if (is_substr(original.get())) { exchange(strl_t(original.get()-get()), original.get_len(), insert); } }
// remove a part of this string
strref remove(strl_t start, strl_t _length) {
set_len_int(_strmod_remove(charstr(), len(), start, _length)); return get_strref(); }
// remove all instances of a character from this string
strref remove(char a) { set_len_int(_strmod_remove(charstr(), len(), a)); return get_strref(); }
// zero terminate this string and return it
const char *c_str() { charstr()[len()<cap()?len():(cap()-1)] = 0; return charstr(); }
// get the end of this string
char* charend() { return charstr()+len(); }
// remove a portion of this string
void erase(strl_t pos, strl_t length) { if (pos<len()) { if ((pos+length)>len())
length = len()-pos; if (length) { for (strl_t i = 0; i<length; i++)
charstr()[pos+i] = charstr()[pos+i+length]; } sub_len_int(length); } }
strmod& cleanup_path() {
set_len(_strmod_cleanup_path(charstr(), get_len()));
return *this;
}
// find a relative path between an original folder and a target file path (returns this as a strref)
strmod& relative_path(strref orig_folder, strref target_path) {
set_len(_strmod_relative_path(charstr(), cap(), orig_folder, target_path));
return *this;
}
};
template <strl_t S> class strown_base {
char string[S];
strl_t length;
protected:
void add_len_int(strl_t l) { length += l; } // unsafe add len (size already checked)
void sub_len_int(strl_t l) { length -= l; } // unsafe sub len (size already checked)
void set_len_int(strl_t l) { length = l; }
void dec_len_int() { length--; }
void inc_len_int() { length++; }
public:
strl_t cap() const { return S; }
char* charstr() { return string; }
const char* charstr() const { return string; }
strl_t len() const { return length; }
};
class strovl_base {
protected:
char *string_ptr;
strl_t string_length;
strl_t string_space;
void add_len_int(strl_t l) { string_length += l; } // unsafe add len (size already checked)
void sub_len_int(strl_t l) { string_length -= l; } // unsafe sub len (size already checked)
void set_len_int(strl_t l) { string_length = l; }
void dec_len_int() { string_length--; }
void inc_len_int() { string_length++; }
public:
strl_t cap() const { return string_space; }
strl_t len() const { return string_length; }
char *charstr() { return string_ptr; }
const char* charstr() const { return string_ptr; }
void invalidate() { string_ptr = nullptr; string_space = 0; }
void set_overlay(char *ptr, strl_t space) { string_ptr = ptr; string_space = space; }
void set_overlay(char *ptr, strl_t space, strl_t len) {
string_ptr = ptr; string_space = space; string_length = len; }
};
// owned string class, instance with 'strown<capacity> name'
template <strl_t S> class strown : public strmod<strown_base<S> > {
public:
strown(const char *s) { strmod<strown_base<S> >::copy(s); }
explicit strown(strref s) { strmod<strown_base<S> >::copy(s); }
strown() {}
};
// overlay string class, instance with 'strovl name(char *, size)'
class strovl : public strmod<strovl_base> {
public:
strovl() { invalidate(); string_length = 0; }
strovl(char *ptr, strl_t space) { set_overlay(ptr, space); string_length = 0; }
strovl(char *ptr, strl_t space, strl_t length) { set_overlay(ptr, space); string_length = length; }
};
// helper for relative strings. purpose is for string collections that may need to grow
// by allocating a new buffer and copying. requires calling get(base strref) tp use string.
class strref_rel {
protected:
strl_t offset;
strl_t length;
public:
strref_rel() { clear(); }
strref_rel(const strref_rel &rel) : offset(rel.offset), length(rel.length) {}
strref_rel(strref orig, strref base) {
if (base.is_substr(orig.get())) {
offset = strl_t(orig.get()-base.get()); length = orig.get_len();
} else
length = 0;
}
strref_rel(const char *str, strl_t len, strref base) {
if (base.is_substr(str)) {
offset = strl_t(str-base.get()); length = len;
} else
length = 0;
}
strref get(strref base) { return strref(base.get() + offset, length); }
strref get(strovl base) { return strref(base.get() + offset, length); }
strl_t get_len() { return length; }
bool valid() const { return length>0; }
operator bool() const { return valid(); }
void clear() { length = 0; }
};
// dynamic collection of strings in a single fixed char array
template <strl_t S> class strcol {
char _buffer[S];
strl_t end_buf;
char* push_back_len(char *w, char *e, strl_t len) { while (w<e && len) { *w++ = (len&0x7f) | (len&(~strl_t(0x7f)) ? 0x80 : 0); len >>= 7; } return w; }
char* push_back_int(const char *s, strl_t l, strl_t o) { char *e = _buffer+S, *w = push_back_len(_buffer+o, e, l); if (strl_t(e-w)<l) return nullptr; memcpy(w, s, l); return w+l; }
strl_t lim(strl_t pos) const { return pos<end_buf ? pos : end_buf; }
strl_t lim_len(strl_t pos, strl_t len) const { return (pos+len)<end_buf ? len : (end_buf-pos); }
public:
strcol() : end_buf(0) { }
bool empty() const { return end_buf == 0; }
void clear() const { end_buf = 0; }
bool end(strl_t curr) const { return curr>=end_buf; }
bool last(strl_t curr) const { return end(next(curr)); }
strl_t get_len(strl_t curr) { strl_t o = 0, s = 0; char c; do { c = _buffer[curr++]; o += strl_t(c&0x7f)<<s; s += 7; } while (c<0); return lim_len(c, o); }
strl_t next(strl_t curr) const { if (curr >= end_buf) return end_buf; strl_t o = 0, s = 0; char c; do { c = _buffer[curr++]; o += strl_t(c&0x7f)<<s; s += 7; } while (c<0); return lim(o+curr); }
strref get(strl_t curr) const { if (end(curr)) return strref(); strl_t o = 0, s = 0, c; do { c = _buffer[curr++]; o += (c&0x7f)<<s; s += 7; } while (c&0x80); return strref(_buffer+curr, lim_len(curr, o)); }
strl_t get_index(int i) const { strl_t curr = 0; while (i-- && !end(curr)) { curr = next(curr); } return curr; }
strref operator[](int i) const { return get(get_index(i)); }
bool push_back(const strref s) { if (char *w = push_back_int(s.get(), s.get_len(), end_buf)) { end_buf = (strl_t)(w-_buffer); return true; } return false; }
void erase(strl_t curr) { strl_t n = next(curr); memmove(_buffer+curr, _buffer+n, end_buf-n); end_buf -= n-curr; }
class iterator {
strcol *coll;
strl_t curr;
public:
iterator() : coll(nullptr), curr(0) {}
iterator(strcol &collection, strl_t pos = 0) : coll(&collection), curr(pos) {}
void operator++() { if (coll) curr = coll->next(curr); else curr = 0; }
bool operator==(const iterator &i) const { return curr==i.curr && coll==i.coll; }
bool operator!=(const iterator &i) const { return curr!=i.curr || coll!=i.coll; }
void erase() { coll->erase(curr); }
strref operator*() { return coll->get(curr); }
};
iterator end() { return iterator(*this, end_buf); }
iterator begin() { return iterator(*this); }
};
#ifdef STRUSE_IMPLEMENTATION
//#include <math.h>
#include <stdlib.h> // atof
// Windows extended ascii: https://msdn.microsoft.com/en-us/library/9hxt0028(v=vs.80).aspx
// Unicode: http://unicode-table.com/en/#basic-latin
// Mac OS Roman ascii: https://en.wikipedia.org/wiki/Mac_OS_Roman
// Amiga ascii: http://www.amigacoding.com/index.php/AMOSi:ASCII_Table
static const uint8_t _aMacOSRomanHigh_ToLower[0x80] = {
0x8a, 0x8c, 0x8d, 0x8e, 0x96, 0x9a, 0x9f, 0x87,
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xbe, 0xbf,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
0xc8, 0xc9, 0xca, 0x88, 0x8a, 0x9b, 0xcf, 0xcf,
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
0xd8, 0xd8, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0x89, 0x90, 0x87,
0x91, 0x8f, 0x92, 0x94, 0x95, 0x93, 0x97, 0x99,
0xf0, 0x98, 0x9c, 0x9e, 0x9d, 0xfd, 0xfe, 0xff };
static const uint8_t _aMacOSRomanHigh_ToUpper[0x80] = {
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0xe7,
0xcb, 0xe5, 0x80, 0xcc, 0x81, 0x82, 0x83, 0xe9,
0xe6, 0xe8, 0xea, 0xed, 0xeb, 0xec, 0x84, 0xee,
0xf1, 0xef, 0x85, 0xcd, 0xf2, 0xf4, 0xf3, 0x86,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xae, 0xaf,
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xce,
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
0xd9, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };
uint8_t int_tolower_macos_roman_ascii(uint8_t c) {
if (c>='A' && c<='Z')
return (uint8_t)(c+'a'-'A');
if (c>=0x80)
return _aMacOSRomanHigh_ToLower[c&0x7f];
return c;
}
uint8_t int_toupper_macos_roman_ascii(uint8_t c) {
if (c>='a' && c<='z')
return (uint8_t)(c+'A'-'a');
if (c>=0x80)
return _aMacOSRomanHigh_ToUpper[c&0x7f];
return c;
}
uint8_t int_tolower_amiga_ascii(uint8_t c) {
if (c>='A' && c<='Z')
return (uint8_t)(c+'a'-'A');
if (c>=0xc0 && c<0xe0)
return (uint8_t)(c+0x20);
return c;
}
uint8_t int_toupper_amiga_ascii(uint8_t c) {
if (c>='a' && c<='z')
return (uint8_t)(c+'A'-'a');
if (c>=0xe0)
return (uint8_t)(c-0x20);
return c;
}
uint8_t int_toupper_win_ascii(uint8_t c) {
if (c<'a')
return c;
if (c<='z')
return (uint8_t)(c+'A'-'a');
switch (c) {
case 0x84:
return 0x83;
case 0x86:
return 0x8f;
case 0x82:
return 0x90;
case 0x91:
return 0x92;
case 0x94:
return 0x99;
case 0x81:
return 0x9a;
}
return c;
}
uint8_t int_tolower_win_ascii(uint8_t c) {
if (c<'A')
return c;
if (c<='Z')
return (uint8_t)(c+'a'-'A');
switch (c) {
case 0x8e:
return 0x84;
case 0x8f:
return 0x86;
case 0x90:
return 0x82;
case 0x92:
return 0x91;
case 0x99:
return 0x94;
case 0x9a:
return 0x81;
}
return c;
}
// General lowercase of unicode range
size_t int_tolower_unicode(size_t c)
{
if (c<'A' || c==0xd7 || c==0x138 || c==0x149)
return c;
if (c<='Z')
return c+'a'-'A';
if (c<0xc0)
return c;
if (c<0xe0 || (c>=0x391 && c<0x3ab) || (c>=0x3d8 && c<0x3f0) || (c>=0x410 && c<0x430))
return c+0x20;
if (c<0x100)
return c;
if (c<0x178) {
if (c>0x138 && c<0x149)
return ((c-1)|1)+1;
return c | 1;
}
if ((c>=0x460 && c<0x482) || (c>=0x48a && c<0x4c0) || (c>=0x4d0 && c<0x530))
return c | 1;
if (c>=0x4c1 && c<0x4cf)
return ((c-1)|1)+1;
if (c>=0x400 && c<0x410)
return c+0x50;
if (c>=0x531 && c<0x556)
return c+0x30;
if (c==0x178)
return 0xff;
if (c==0x4c0)
return 0x4cf;
return c;
}
// General uppercase of unicode range
size_t int_toupper_unicode(size_t c)
{
if (c<'a' || c==0xd7 || c==0x138 || c==0x149)
return c;
if (c<='z')
return c+'A'-'c';
if (c<0xe0)
return c;
if (c==0xff)
return 0x178;
if (c<0x100 || (c>=0x3b1 && c<0x3cb) || (c>=0x3f8 && c<0x410) || (c>=0x430 && c<0x450))
return c-0x20;
if (c<0x178) {
if (c>0x138 && c<0x149)
return ((c-1)&1UL)+1;
return c & ~1UL;
}
if ((c>=0x460 && c<0x482) || (c>=0x48a && c<0x4c0) || (c>=0x4d0 && c<0x530))
return c & ~1UL;
if (c>=0x4c1 && c<0x4cf)
return ((c-1)&1UL)+1;
if (c>=0x450 && c<0x460)
return c-0x50;
if (c>=0x561 && c<0x586)
return c-0x30;
if (c==0x4cf)
return 0x4c0;
return c;
}
// english latin lowercase
uint8_t int_tolower_ascii7(uint8_t c)
{
if (c<='Z' && c>='A')
return (uint8_t)(c+'a'-'A');
return c;
}
// english latin uppercase
uint8_t int_toupper_ascii7(uint8_t c)
{
if (c>='a' && c<='z')
return (uint8_t)(c+'A'-'a');
return c;
}
// convert escape codes to characters
// supports: \a, \b, \f, \n, \r, \t, \v, \000, \x00
// any other character is returned as same
static strl_t int_get_esc_code(const uint8_t *buf, strl_t left, uint8_t &code)
{
strl_t step = 0;
if (!left)
return step;
uint8_t c = *buf++;
left--;
step++;
if (c=='x' && left && strref::is_hex(*buf)) {
// parse hex char code
c = 0;
for (int r = 0; r<2 && left; r++) {
uint8_t n = *buf++;
if (!strref::is_hex(n))
break;
c = (uint8_t)((c<<4) + n - (n<='9' ? '0' : (n<='F'?('A'-0xA) : ('a'-0xa))));
step++;
left--;
}
} else if (c>='0' && c<='7') {
// parse octal char code
c -= '0';
for (int r = 0; r<2 && left; r++) {
uint8_t n = *buf++;
if (n<'0' || n>'7')
break;
c = (uint8_t)(c*8 + n-'0');
step++;
left--;
}
} else {
// check for custom escape code symbol
switch (c) {
case 'a':
c = 7;
break;
case 'b':
c = 8;
break;
case 'f':
c = 12;
break;
case 'n':
c = 10;
break;
case 'r':
c = 13;
break;
case 't':
c = 9;
break;
case 'v':
c = 11;
break;
}
}
code = c;
return step;
}
// tolower/toupper implementation
char strref::tolower(char c) { return (char)int_tolower_ascii7((uint8_t)c); }
char strref::toupper(char c) { return (char)int_toupper_ascii7((uint8_t)c); }
char strref::tolower_win(char c) { return (char)int_tolower_win_ascii((uint8_t)c); }
char strref::toupper_win(char c) { return (char)int_toupper_win_ascii((uint8_t)c); }
char strref::tolower_amiga(char c) { return (char)int_tolower_amiga_ascii((uint8_t)c); }
char strref::toupper_amiga(char c) { return (char)int_toupper_amiga_ascii((uint8_t)c); }
char strref::tolower_macos(char c) { return (char)int_tolower_macos_roman_ascii((uint8_t)c); }
char strref::toupper_macos(char c) { return (char)int_toupper_macos_roman_ascii((uint8_t)c); }
size_t strref::tolower_unicode(int c) { return int_tolower_unicode((size_t)c); }
size_t strref::toupper_unicode(int c) { return int_toupper_unicode((size_t)c); }
// use printf to print current string on a single line
void strref::writeln()
{
if (valid()) {
printf(STRREF_FMT "\n", STRREF_ARG(*this));
} else
printf("\n");
}
// construct a strref from const char*
void strref::set(const char *str)
{
if (!str || !*str) {
string = nullptr;
length = 0;
} else {
string = str;
strl_t l = 0;
while (*str++)
l++;
length = l;
}
}
// get fnv1a hash of a string
unsigned int strref::fnv1a(unsigned int seed) const
{
unsigned int hash = seed;
if (string) {
unsigned const char *scan = (unsigned const char*)string;
strl_t left = length;
while (left--)
hash = (*scan++ ^ hash) * 16777619;
}
return hash;
}
// get 16 bit fnv1a hash of a string, this is officially done by xor folding
unsigned short strref::fnv1a_16(unsigned int seed) const
{
unsigned int hash = fnv1a(seed);
return (unsigned short)(hash ^ (hash>>16));
}
uint64_t strref::fnv1a_64(uint64_t seed) const
{
uint64_t hash = seed;
if (string) {
unsigned const char *scan = (unsigned const char*)string;
strl_t left = length;
while (left--)
hash = (*scan++ ^ hash) * 1099511628211;
}
return hash;
}
// get lowercase fnv1a hash of a string
unsigned int strref::fnv1a_lower(unsigned int seed) const
{
unsigned const char *scan = (unsigned const char*)string;
unsigned int hash = seed;
strl_t left = length;
while (left--)
hash = (int_toupper_ascii7(*scan++) ^ hash) * 16777619;
return hash;
}
// get fnv1a hash of a string and treat any number whitespace as a single space
unsigned int strref::fnv1a_ws(unsigned int seed) const
{
unsigned const char *scan = (unsigned const char*)string;
unsigned int hash = seed;
strl_t left = length;
while (left--) {
uint8_t c = *scan++;
if (c<' ')
c = ' ';
hash = (c ^ hash) * 16777619;
if (c==' ') {
while (left && *scan<=0x20) {
left--;
scan++;
}
}
}
return hash;
}
// convert numeric string to integer
int64_t strref::atoi() const
{
if (string) {
const unsigned char *s = get_u();
const unsigned char *e = s + length;
while (s!=e && *s<=0x20) s++;
if (s<e) {
int64_t v = 0;
bool neg = *s=='-';
if (neg) s++;
while (s!=e) {
unsigned char c = *s++;
if (c<'0' || c>'9') break;
v = c-'0' + v*10;
}
return neg ? -v : v;
}
}
return 0;
}
uint64_t strref::atoui() const
{
if (string) {
const unsigned char *s = get_u();
strl_t left = length;
while (left && *s<=0x20) { s++; left--; }
if (left>=2 && *s=='0' && s[1]=='x' ) { return ahextou64(); }
uint64_t v = 0;
while (left) {
unsigned char c = *s++;
if (c<'0' || c>'9') break;
v = c-'0' + v*10;
--left;
}
return v;
}
return 0;
}
// convert numeric string into floating point value
float strref::atof() const {
if (string[length]==0)
return (float)::atof(string);
strown<64> num(*this);
return (float)::atof(num.c_str());
}
// convert numeric string into double precision floating point value
double strref::atod() const {
if (string[length]==0)
return ::atof(string);
strown<64> num(*this);
return ::atof(num.c_str());
}
// convert numeric string to integer and move string forward
int strref::atoi_skip()
{
const char *scan = string;
strl_t left = length;
while (*scan<=0x20 && left) {
scan++;
left--;
}
if (!left)
return 0;
bool neg = false;
if (*scan=='-') {
neg = true;
left--;
}
int value = 0;
while (left) {
char c = *scan;
if (c<'0' || c>'9')
break;
left--;
scan++;
value = c-'0' + value*10;
}
string += length-left;
length = left;
return neg ? -value : value;
}
// convert a hexadecimal string to an unsigned integer
size_t strref::ahextoui() const
{
const char *scan = string;
strl_t left = length;
while (left && *scan<=0x20) {
scan++;
left--;
}
if (!left)
return 0;
if (left>2 && *scan=='0' && (scan[1]=='x' || scan[1]=='X')) {
scan += 2;
left -= 2;
}
strl_t hex = 0;
while (left) {
char c = *scan++;
left--;
if (c>='0' && c<='9')
hex = (hex<<4) | (c-'0');
else if (c>='a' && c<='f')
hex = (hex<<4) | (c-'a'+10);
else if (c>='A' && c<='F')
hex = (hex<<4) | (c-'A'+10);
else
break;
}
return hex;
}
// convert a hexadecimal string to an unsigned integer
uint64_t strref::ahextou64() const
{
const char *scan = string;
strl_t left = length;
while (*scan<=0x20 && left) {
scan++;
left--;
}
if (!left)
return 0;
if (left>2 && *scan=='0' && (scan[1]=='x' || scan[1]=='X')) {
scan += 2;
left -= 2;
}
uint64_t hex = 0;
while (left) {
char c = *scan++;
left--;
if (c>='0' && c<='9')
hex = (hex<<4) | (c-'0');
else if (c>='a' && c<='f')
hex = (hex<<4) | (c-'a'+10);
else if (c>='A' && c<='F')
hex = (hex<<4) | (c-'A'+10);
else
break;
}
return hex;
}
// convert a hexadecimal string to an unsigned integer
size_t strref::ahextoui_skip()
{
const char *scan = string;
strl_t left = length;
while (left && *scan<=0x20) {
scan++;
left--;
}
if (!left)
return 0;
if (left>2 && *scan=='0' && (scan[1]=='x' || scan[1]=='X')) {
scan += 2;
left -= 2;
}
if (left > 16) { left = 16; }
size_t hex = 0;
while (left) {
char c = *scan;
if (c>='0' && c<='9')
hex = (hex<<4) | (c-'0');
else if (c>='a' && c<='f')
hex = (hex<<4) | (c-'a'+10);
else if (c>='A' && c<='F')
hex = (hex<<4) | (c-'A'+10);
else
break;
scan++;
left--;
}
length -= strl_t(scan-string);
string = scan;
return hex;
}
// convert a binary string to an unsigned integer
size_t strref::abinarytoui_skip()
{
skip_whitespace();
const char *scan = string;
strl_t left = length;
if (!left)
return 0;
strl_t bin = 0;
while (left) {
uint8_t c = (uint8_t)*scan;
if (c<'0' || c>'1')
break;
scan++;
left--;
bin = (bin<<1) | (c-'0');
}
length -= strl_t(scan-string);
string = scan;
return bin;
}
// convert a hexadecimal string to a signed integer
int strref::ahextoi() const
{
const char *scan = string;
strl_t left = length;
while (*scan<=0x20 && left) {
scan++;
left--;
}
if (!left)
return 0;
bool neg = *scan=='-';
if (neg) {
scan++;
left--;
}
if (left>2 && *scan=='0' && (scan[1]=='x' || scan[1]=='X')) {
scan += 2;
left -= 2;
}
strl_t hex = 0;
while (left) {
char c = *scan++;
left--;
if (c>='0' && c<='9')
hex = (hex<<4) | (c-'0');
else if (c>='a' && c<='f')
hex = (hex<<4) | (c-'a'+10);
else if (c>='A' && c<='F')
hex = (hex<<4) | (c-'A'+10);
else
break;
}
return neg ? -(int)hex : (int)hex;
}
// count instances of a character in a string
int strref::count_char(char c) const
{
strl_t left = length;
const char *scan = string;
int count = 0;
while (left) {
if (*scan++ == c)
count++;
left--;
}
return count;
}
// skip bom header of a utf-8 file if detected
strref strref::skip_bom()
{
const uint8_t* buf = get_u();
if (length >= 3 && buf && buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf) {
return strref(string + 3, length - 3);
}
return *this;
}
// find a character in a string
static int int_find_char(char c, const char *scan, strl_t length)
{
strl_t left = length;
while (left) {
if (*scan++ == c)
return int(length - left);
left--;
}
return -1;
}
// find a character in a string after pos
int strref::find(char c) const
{
if (!valid())
return -1;
return int_find_char(c, string, length);
}
// find an instance of a char after pos
int strref::find_after(char c, strl_t pos) const
{
if (length>pos) {
int o = int_find_char(c, string + pos + 1, length - pos - 1);
if (o >= 0)
return int(o + pos + 1);
}
return -1;
}
// find an instance of a char at pos or after
int strref::find_at(char c, strl_t pos) const
{
if (length>pos) {
int o = int_find_char(c, string + pos, length - pos);
if (o >= 0)
return int(o + pos);
}
return -1;
}
// find an instance of a char at pos or after or return full string
strl_t strref::find_or_full(char c, strl_t pos) const
{
if (!string)
return 0;
if (pos>=length)
return length;
int o = int_find_char(c, string + pos, length - pos);
if (o >= 0)
return o + pos;
return length;
}
// ignore matches that are in escape codes
strl_t strref::find_or_full_esc(char c, strl_t pos) const
{
if (!string)
return 0;
if (pos>=length)
return length;
strl_t left = length-pos;
const char *scan = string+pos;
while (left) {
char m = *scan++;
if (m=='\\' && left) {
scan++;
left--;
} else if (m==c)
return length-left;
left--;
}
return length;
}
// find last position of character c
int strref::find_last(char c) const
{
if (length && string) {
strl_t left = length;
const char *scan = string + left;
while (left) {
if (*--scan == c)
return int(left - 1);
left--;
}
}
return -1;
}
// find first position of either c or d
int strref::find(char c, char d) const
{
strl_t left = length;
if (const char *scan = string) {
while (left) {
char n = *scan++;
if (n == c || n == d)
return int(length - left);
left--;
}
}
return -1;
}
// find last instance of either character c or d
int strref::find_last(char c, char d) const
{
if (length && string) {
strl_t left = length - 1;
const char *scan = string + left;
while (left) {
char n = *--scan;
if (n == c || n == d)
return int(left - 1);
left--;
}
}
return -1;
}
int strref::find_skip_parens(char token) const
{
int parens = 0;
const char* scan = string;
strl_t left = length;
while (left && (parens || *scan != token)) {
if (*scan == '(') { ++parens; } else if (*scan == ')' && parens) { --parens; }
--left;
++scan;
}
if (left) { return length - left; }
return -1;
}
// compare a string with a substring case sensitive
static bool int_compare_substr_case(const char *scan, strl_t length, const char *check, strl_t chk_len)
{
if (length < chk_len)
return false;
if (scan==nullptr || check==nullptr)
return scan==check;
for (strl_t cl = 0; cl<chk_len; cl++) {
if (*scan++ != *check++)
return false;
}
return true;
}
// compare a string with a substring case sensitive
static bool int_compare_substr(const uint8_t *scan, strl_t length, const uint8_t *check, strl_t chk_len)
{
if (length < chk_len)
return false;
for (strl_t cl = 0; cl<chk_len; cl++) {
if (int_tolower_ascii7(*scan++) != int_tolower_ascii7(*check++))
return false;
}
return true;
}
// case sensitive rolling hash find substring
int _find_rh_case(const char *text, strl_t length, const char *comp, strl_t comp_length)
{
if (length < comp_length)
return -1;
size_t roll_hash = 0;
size_t hash = 0;
size_t hash_remove = 1;
const char *rem = text;
// generate compare hash
for (strl_t cl = 0; cl<comp_length; cl++) {
hash = hash * 101 + (size_t)comp[cl];
hash_remove *= 101;
}
// initial buffer
for (strl_t cl = 0; cl<comp_length; cl++)
roll_hash = roll_hash*101 + (size_t)*text++;
// roll
strl_t left = length - comp_length;
for (;;) {
if (roll_hash == hash) {
// compare!
if (int_compare_substr_case(text - comp_length, left + comp_length, comp, comp_length))
return int(length - comp_length - left);
}
if (!left)
break;
left--;
roll_hash = roll_hash * 101 + (size_t)*text++ -
hash_remove * (size_t)*rem++;
}
return -1;
}
// case ignore rolling hash find substring
int _find_rh(const uint8_t *text, strl_t length, const uint8_t *comp, strl_t comp_length)
{
if (length < comp_length)
return -1;
size_t roll_hash = 0;
size_t hash = 0;
size_t hash_remove = 1;
const uint8_t *rem = text;
// generate compare hash
for (strl_t cl = 0; cl<comp_length; cl++) {
hash = hash*101 + int_tolower_ascii7(comp[cl]);
hash_remove *= 101;
}
// initial buffer
for (strl_t cl = 0; cl<comp_length; cl++)
roll_hash = roll_hash*101 + int_tolower_ascii7(*text++);
// roll
strl_t left = length - comp_length;
for (;;) {
if (roll_hash == hash) {
// compare!
if (int_compare_substr(text - comp_length, left + comp_length, comp, comp_length))
return int(left - length + comp_length);
}
if (!left)
break;
left--;
roll_hash = roll_hash*101 + int_tolower_ascii7(*text++) -
hash_remove * int_tolower_ascii7(*rem++);
}
return -1;
}
// calculate number of characters in this line
strl_t strref::len_eol() const
{
strl_t left = length;
const char *str = string;
if (!left || !str)
return 0;
while (left && *str!=0x0d && *str!=0x0a) {
str++;
left--;
}
return length-left;
}
// length of string with escape characters counting as one
strl_t strref::len_esc() const
{
strl_t len = 0;
if (const char *str = string) {
strl_t left = length;
while (left) {
left--;
if (*str=='\\' && left) {
left--;
str++;
}
str++;
len++;
}
}
return len;
}
// calculate number of characters to next line
strl_t strref::len_next_line() const
{
strl_t line = len_eol();
if (line < length) {
if (string[line]==0x0d) {
line++;
if (line < length && string[line]==0x0a)
line++;
} else if (string[line]==0x0a) {
line++;
if (string[line]==0x0d)
line++;
}
}
return line;
}
// compare two strings case ignored
bool strref::same_str(const strref str) const
{
if (length!=str.length)
return false;
const uint8_t *scan = get_u();
const uint8_t *compare = str.get_u();
if (scan==nullptr || compare==nullptr)
return scan==compare;
strl_t compare_left = length;
while (compare_left) {
if (int_tolower_ascii7(*scan++)!=int_tolower_ascii7(*compare++))
return false;
compare_left--;
}
return true;
}
// mid string compare
bool strref::same_substr(const strref str, strl_t pos) const {
if ((str.length+pos) > length)
return false;
return int_compare_substr(get_u() + pos, length - pos, str.get_u(), str.length);
}
// allow escape codes in search string
bool strref::same_substr_esc(const strref str, strl_t pos) const {
if (pos >= length)
return false;
const uint8_t *scan = get_u() + pos;
const uint8_t *compare = str.get_u();
strl_t compare_left = str.length;
while (compare_left) {
uint8_t c = (uint8_t)*compare++;
compare_left--;
if (c=='\\' && compare_left) {
strl_t skip = int_get_esc_code(compare, compare_left, c);
compare += skip;
compare_left -= skip;
}
if (int_tolower_ascii7(*scan++) != int_tolower_ascii7(c))
return false;
}
return true;
}
// case sensitive substring compare
bool strref::same_substr_case(const strref str, strl_t pos) const {
if ((str.length+pos) > length)
return false;
return int_compare_substr_case(string + pos, length - pos, str.string, str.length);
}
// allow escape codes in search string
bool strref::same_substr_case_esc(const strref str, strl_t pos) const {
if (pos >= length)
return false;
const uint8_t *scan = get_u() + pos;
const uint8_t *compare = str.get_u();
strl_t compare_left = str.length;
while (compare_left) {
uint8_t c = (uint8_t)*compare++;
compare_left--;
if (c=='\\' && compare_left) {
strl_t skip = int_get_esc_code(compare, compare_left, c);
compare += skip;
compare_left -= skip;
}
if (*scan++ != c)
return false;
}
return true;
}
// iterate over tokens to find a string that matches substr case ignored
strref strref::find_token(const char *substr, char token) const
{
strref parse = *this;
while (strref chunk = parse.token_chunk(token)) {
if (chunk.same_str(substr))
return chunk;
parse.token_skip(chunk);
}
return strref();
}
// iterate over tokens to find a string matching substr case ignored
strref strref::find_token(strref substr, char token) const
{
strref parse = *this;
while (strref chunk = parse.token_chunk(token)) {
if (chunk.same_str(substr))
return chunk;
parse.token_skip(chunk);
}
return strref();
}
// iterate over tokens to find a string matching substr case sensitive
strref strref::find_token_case(const char *substr, char token) const
{
strref parse = *this;
while (strref chunk = parse.token_chunk(token)) {
if (chunk.same_str_case(substr))
return chunk;
parse.token_skip(chunk);
}
return strref();
}
// iterate over tokens to find a string matching substr case sensitive
strref strref::find_token_case(strref substr, char token) const
{
strref parse = *this;
while (strref chunk = parse.token_chunk(token)) {
if (chunk.same_str_case(substr))
return chunk;
parse.token_skip(chunk);
}
return strref();
}
// determine if string is greater than other string
bool strref::operator>(const strref o) const
{
const char *scan = string;