comparison foosdk/sdk/pfc/string-interface.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 namespace pfc {
4 inline t_size _strParamLen(const char* str) { return strlen(str); }
5
6 class NOVTABLE string_receiver {
7 public:
8 virtual void add_string(const char* p_string, t_size p_string_size = SIZE_MAX) = 0;
9 inline void add_string_(const char* str) { add_string(str, _strParamLen(str)); }
10
11 void add_char(t_uint32 c);//adds unicode char to the string
12 void add_byte(char c) { add_string(&c, 1); }
13 void add_chars(t_uint32 p_char, t_size p_count) { for (; p_count; p_count--) add_char(p_char); }
14 protected:
15 string_receiver() {}
16 ~string_receiver() {}
17 };
18
19 class NOVTABLE string_base : public pfc::string_receiver {
20 public:
21 virtual const char* get_ptr() const = 0;
22 const char* c_str() const { return get_ptr(); }
23 virtual void add_string(const char* p_string, t_size p_length = SIZE_MAX) = 0;//same as string_receiver method
24 virtual void set_string(const char* p_string, t_size p_length = SIZE_MAX) { reset(); add_string(p_string, p_length); }
25 virtual void truncate(t_size len) = 0;
26 virtual t_size get_length() const { return strlen(get_ptr()); }
27 virtual char* lock_buffer(t_size p_requested_length) = 0;
28 virtual void unlock_buffer() = 0;
29
30 void set_string_(const char* str) { set_string(str, _strParamLen(str)); }
31
32 inline const char* toString() const { return get_ptr(); }
33
34 //! For compatibility with old conventions.
35 inline t_size length() const { return get_length(); }
36
37 inline void reset() { truncate(0); }
38 inline void clear() { truncate(0); }
39
40 inline bool is_empty() const { return *get_ptr() == 0; }
41
42 void skip_trailing_chars(const char* lstChars);
43 void skip_trailing_char(unsigned c = ' ');
44
45 bool is_valid_utf8() const;
46
47 void convert_to_lower_ascii(const char* src, char replace = '?');
48
49 inline const string_base& operator= (const char* src) { set_string_(src); return *this; }
50 inline const string_base& operator+= (const char* src) { add_string_(src); return *this; }
51 inline const string_base& operator= (const string_base& src) { set_string(src); return *this; }
52 inline const string_base& operator+= (const string_base& src) { add_string(src); return *this; }
53
54 bool operator==(const string_base& p_other) const { return strcmp(*this, p_other) == 0; }
55 bool operator!=(const string_base& p_other) const { return strcmp(*this, p_other) != 0; }
56 bool operator>(const string_base& p_other) const { return strcmp(*this, p_other) > 0; }
57 bool operator<(const string_base& p_other) const { return strcmp(*this, p_other) < 0; }
58 bool operator>=(const string_base& p_other) const { return strcmp(*this, p_other) >= 0; }
59 bool operator<=(const string_base& p_other) const { return strcmp(*this, p_other) <= 0; }
60
61 inline operator const char* () const { return get_ptr(); }
62
63 t_size scan_filename() const;
64
65 t_size find_first(char p_char, t_size p_start = 0) const;
66 t_size find_last(char p_char, t_size p_start = SIZE_MAX) const;
67 t_size find_first(const char* p_string, t_size p_start = 0) const;
68 t_size find_last(const char* p_string, t_size p_start = SIZE_MAX) const;
69
70 void fix_dir_separator(char c = '\\'); // Backwards compat function, "do what I mean" applied on non Windows
71 void end_with(char c);
72 void end_with_slash();
73 bool ends_with(char c) const;
74 void delimit(const char* c) { if (length() > 0) add_string(c); }
75 char last_char() const;
76 void truncate_last_char();
77 void truncate_number_suffix();
78
79 //! Truncates string at first encountered end-of-line mark, starting search from startBytes position, in bytes..
80 bool truncate_eol(t_size startBytes = 0);
81 //! Truncates string at first encountered end-of-line mark, starting search from startBytes position, in bytes.
82 //! Adds append value if string was altered.
83 bool fix_eol(const char* append = " (...)", t_size startBytes = 0);
84 //! Limits string length to the specified value in actual characters.
85 //! That is, multi-byte UTF-8 characters will be counted as one and never broken apart.
86 bool limit_length(t_size length_in_chars, const char* append = " (...)");
87
88 void truncate_filename() { truncate(scan_filename()); }
89 void truncate_to_parent_path();
90 void add_filename(const char* fn) { end_with_slash(); *this += fn; }
91
92 //! Replaces one string with another. Returns the number of occurances - zero if the string was not altered.
93 size_t replace_string(const char* replace, const char* replaceWith, t_size start = 0);
94 //! Replaces one string with another, writing the output to another string object. \n
95 //! Returns the number of occurances replaced. \n
96 //! Special: returns zero if no occurances were found - and the target string is NOT modified if so. Use with care!
97 size_t replace_string_ex(pfc::string_base& target, const char* replace, const char* replaceWith, t_size start = 0) const;
98
99 string_base& _formatter() const { return const_cast<string_base&>(*this); }
100
101 bool has_prefix(const char* prefix) const;
102 bool has_prefix_i(const char* prefix) const;
103 bool has_suffix(const char* suffix) const;
104 bool has_suffix_i(const char* suffix) const;
105
106 bool equals(const char* other) const;
107 protected:
108 string_base() {}
109 ~string_base() {}
110 };
111 }