comparison foosdk/sdk/pfc/string-lite.h @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #pragma once
2
3 #include "string-interface.h"
4 #include "mem_block.h"
5 #include "string-compare.h"
6
7 namespace pfc {
8
9 typedef const char* stringp;
10 inline size_t stringp_length(stringp s) noexcept { return strlen(s); }
11
12 class stringLite : public string_base {
13 public:
14 struct tagNoShrink {};
15 struct tagPrealloc { size_t amount; };
16 stringLite(tagNoShrink) { this->setNoShrink(); }
17 stringLite(tagPrealloc const& tag) { this->prealloc(tag.amount); }
18 stringLite() {}
19 stringLite( const stringLite & other ) { copy(other); }
20 stringLite( stringLite && other ) noexcept { move(other); }
21 stringLite( const char * p, size_t len = SIZE_MAX ) { set_string( p, len ); }
22 stringLite( string_part_ref const & );
23 stringLite( string_base const& );
24
25 stringLite const & operator=( const stringLite & other ) { copy(other); return *this; }
26 stringLite const & operator=( stringLite && other ) noexcept { move(other); return *this; }
27 stringLite const & operator=( const char * p ) { set_string( p ); return *this; }
28 stringLite const & operator=( const string_part_ref & );
29 stringLite const & operator=( const string_base & );
30
31 const char * get_ptr() const { return m_ptr; }
32 operator const char*() const { return m_ptr; }
33 const char* c_str() const { return m_ptr; }
34 const char* ptr() const { return m_ptr; }
35 void add_string(const char * p_string,t_size p_string_size = SIZE_MAX);
36 void set_string(const char * p_string,t_size p_length = SIZE_MAX);
37 void add_string_nc( const char * ptr, size_t length );
38 void set_string_nc( const char * ptr, size_t length );
39 void truncate(t_size len);
40 t_size get_length() const { return m_length; }
41 char * lock_buffer(t_size p_requested_length);
42 void unlock_buffer();
43 void set_char(size_t offset,char c);
44 void prealloc(size_t);
45 void setNoShrink(bool v = true) { m_noShrink = v; }
46
47 size_t replace_nontext_chars(char p_replace = '_');
48 size_t replace_char(unsigned c1,unsigned c2);
49 size_t replace_byte(char c1,char c2);
50 void remove_chars(t_size first,t_size count);
51 void insert_chars(t_size first,const char * src, t_size count);
52 void insert_chars_nc(t_size first,const char * src, t_size count);
53 void insert_chars(t_size first,const char * src);
54
55 bool operator==(const stringLite& other) const noexcept;
56 bool operator!=(const stringLite& other) const noexcept;
57 bool operator==(const char* other) const noexcept;
58 bool operator!=(const char* other) const noexcept;
59 bool operator>(const stringLite& other) const noexcept;
60 bool operator<(const stringLite& other) const noexcept;
61 bool operator>(const char* other) const noexcept;
62 bool operator<(const char* other) const noexcept;
63 bool operator>=(const stringLite& other) const noexcept;
64 bool operator<=(const stringLite& other) const noexcept;
65 bool operator>=(const char* other) const noexcept;
66 bool operator<=(const char* other) const noexcept;
67
68
69 stringLite const& operator+=(const stringLite& other) { add_string(other.c_str(), other.length()); return *this; }
70 stringLite const& operator+=(const char* other) { add_string(other); return *this; }
71 stringLite const& operator+=(string_part_ref other) { add_string_nc(other.m_ptr, other.m_len); return *this; }
72
73 stringLite operator+(const stringLite& other) const {stringLite ret = *this; ret += other; return ret;}
74 stringLite operator+(const char * other) const { stringLite ret = *this; ret += other; return ret; }
75 stringLite operator+(string_part_ref other) const { stringLite ret = *this; ret += other; return ret; }
76
77 bool equals(const stringLite& other) const noexcept;
78 bool equals(const char* other) const noexcept;
79 static bool equals( const stringLite & v1, const stringLite & v2 ) noexcept;
80 static bool greater( const stringLite & v1, const stringLite & v2 ) noexcept;
81 static bool greater( const char * v1, size_t s1, const char * v2, size_t s2) noexcept;
82
83 void clear() noexcept;
84 void copy( stringLite const & other );
85 void move( stringLite & other ) noexcept;
86
87 stringLite lowerCase() const;
88 stringLite upperCase() const;
89 stringLite toLower() const { return lowerCase(); }
90 stringLite toUpper() const { return upperCase(); }
91
92 typedef stringComparatorCaseSensitive comparatorCaseSensitive;
93 typedef stringComparatorCaseInsensitive comparatorCaseInsensitive;
94 typedef stringComparatorCaseInsensitiveASCII comparatorCaseInsensitiveASCII;
95
96 static bool g_equals(const stringLite& p_item1, const stringLite& p_item2) { return p_item1 == p_item2; }
97 static bool g_equalsCaseInsensitive(const stringLite& p_item1, const stringLite& p_item2) { return comparatorCaseInsensitive::compare(p_item1, p_item2) == 0; }
98
99 stringLite subString(t_size base) const;
100 stringLite subString(t_size base, t_size count) const;
101 static bool isNonTextChar(char c) { return c >= 0 && c < 32; }
102
103 //! @returns SIZE_MAX if not found.
104 size_t indexOf(char c, t_size base = 0) const;
105 //! @returns SIZE_MAX if not found.
106 size_t lastIndexOf(char c, size_t base = SIZE_MAX) const;
107 //! @returns SIZE_MAX if not found.
108 size_t indexOf(stringp s, size_t base = 0) const;
109 //! @returns SIZE_MAX if not found.
110 size_t lastIndexOf(stringp s, size_t base = SIZE_MAX) const;
111 //! @returns SIZE_MAX if not found.
112 size_t indexOfAnyChar(stringp s, size_t base = 0) const;
113 //! @returns SIZE_MAX if not found.
114 size_t lastIndexOfAnyChar(stringp s, size_t base = SIZE_MAX) const;
115
116 bool contains(char c) const;
117 bool contains(stringp s) const;
118
119 bool containsAnyChar(stringp s) const;
120
121 bool startsWith(char c) const;
122 bool startsWith(stringp s) const;
123 bool endsWith(char c) const;
124 bool endsWith(stringp s) const;
125
126 char firstChar() const;
127 char lastChar() const;
128
129 bool isEmpty() const { return empty(); }
130 bool empty() const { return length() == 0;}
131
132 stringLite replace(stringp strOld, stringp strNew) const;
133 stringLite trim(char c) const;
134 private:
135 void _clear() noexcept;
136 void makeRoom( size_t size );
137 const char * m_ptr = "";
138 size_t m_length = 0;
139 mem_block m_mem;
140 bool m_noShrink = false;
141 };
142
143 typedef stringLite string8;
144 typedef stringLite string_formatter;
145 }