1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************
astring.h
Allocated string manipulation functions.
***************************************************************************/
#pragma once
#ifndef __ASTRING_H__
#define __ASTRING_H__
#include <stdarg.h>
#include <ctype.h>
#include "osdcomm.h"
//**************************************************************************
// TYPE DEFINITIONS
//**************************************************************************
// basic allocated string class
class astring
{
public:
// simple construction/destruction
astring() { init(); }
~astring();
// construction with copy
astring(const char *string) { init(); if(string) cpy(string); }
astring(const char *string, int length) { init().cpy(string, length); }
astring(const astring &string) { init().cpy(string); }
astring(const astring &string, int start, int count = -1) { init().cpysubstr(string, start, count); }
// assignment operators
astring &operator=(const char *string) { return cpy(string); }
astring &operator=(const astring &string) { return cpy(string); }
// concatenation operators
astring& operator+=(const astring &string) { return cat(string); }
friend astring operator+(const astring &lhs, const astring &rhs) { return astring(lhs) += rhs; }
friend astring operator+(const astring &lhs, const char *rhs) { return astring(lhs) += rhs; }
friend astring operator+(const char *lhs, const astring &rhs) { return astring(lhs) += rhs; }
// comparison operators
bool operator==(const char *string) const { return (cmp(string) == 0); }
bool operator==(const astring &string) const { return (cmp(string) == 0); }
bool operator!=(const char *string) const { return (cmp(string) != 0); }
bool operator!=(const astring &string) const { return (cmp(string) != 0); }
bool operator<(const char *string) const { return (cmp(string) < 0); }
bool operator<(const astring &string) const { return (cmp(string) < 0); }
bool operator<=(const char *string) const { return (cmp(string) <= 0); }
bool operator<=(const astring &string) const { return (cmp(string) <= 0); }
bool operator>(const char *string) const { return (cmp(string) > 0); }
bool operator>(const astring &string) const { return (cmp(string) > 0); }
bool operator>=(const char *string) const { return (cmp(string) >= 0); }
bool operator>=(const astring &string) const { return (cmp(string) >= 0); }
// character access operators
char operator[](int index) const { return (index < len()) ? m_text[index] : 0; }
// implicit boolean conversion operators
operator bool() { return m_text[0] != 0; }
operator bool() const { return m_text[0] != 0; }
// C string conversion operators and helpers
const char *c_str() const { return m_text; }
// buffer management
astring &reset() { return cpy(""); }
astring &expand(int length) { ensure_room(length); return *this; }
// length query
int len() const { return m_len; }
// copy helpers
astring &cpy(const char *src, int count);
astring &cpysubstr(const astring &src, int start, int count = -1);
astring &cpy(const astring &src) { return cpy(src.c_str(), src.len()); }
astring &cpy(const char *src) { return cpy(src, strlen(src)); }
// insertion helpers
astring &ins(int insbefore, const char *src, int count);
astring &inssubstr(int insbefore, const astring &src, int start, int count = -1);
astring &ins(int insbefore, const astring &src) { return ins(insbefore, src.c_str(), src.len()); }
astring &ins(int insbefore, const char *src) { return ins(insbefore, src, strlen(src)); }
// concatenation helpers (== insert at end)
astring &cat(const char *src, int count) { return ins(-1, src, count); }
astring &catsubstr(const astring &src, int start, int count = -1) { return inssubstr(-1, src, start, count); }
astring &cat(const astring &src) { return ins(-1, src.c_str(), src.len()); }
astring &cat(const char *src) { return ins(-1, src, strlen(src)); }
astring &cat(char ch) { return ins(-1, &ch, 1); }
// substring helpers
astring &substr(int start, int count = -1);
astring &del(int start, int count = -1);
// formatted string helpers
int vprintf(const char *format, va_list args);
int catvprintf(const char *format, va_list args);
int printf(const char *format, ...) ATTR_PRINTF(2,3) { va_list ap; va_start(ap, format); int result = this->vprintf(format, ap); va_end(ap); return result; }
int catprintf(const char *format, ...) ATTR_PRINTF(2,3) { va_list ap; va_start(ap, format); int result = catvprintf(format, ap); va_end(ap); return result; }
astring &format(const char *format, ...) ATTR_PRINTF(2,3) { va_list ap; va_start(ap, format); this->vprintf(format, ap); va_end(ap); return *this; }
astring &catformat(const char *format, ...) ATTR_PRINTF(2,3) { va_list ap; va_start(ap, format); catvprintf(format, ap); va_end(ap); return *this; }
// comparison helpers
int cmp(const char *str2, int count) const;
int cmpsubstr(const astring &str2, int start, int count = -1) const;
int cmp(const astring &str2) const { return cmp(str2.c_str(), str2.len()); }
int cmp(const char *str2) const { return cmp(str2, strlen(str2)); }
// case-insensitive comparison helpers
int icmp(const char *str2, int count) const;
int icmpsubstr(const astring &str2, int start, int count = -1) const;
int icmp(const astring &str2) const { return icmp(str2.c_str(), str2.len()); }
int icmp(const char *str2) const { return icmp(str2, strlen(str2)); }
// character searching helpers
int chr(int start, int ch) const;
int rchr(int start, int ch) const;
// string searching/replacing helpers
int find(int start, const char *search) const;
int find(const char *search) const { return find(0, search); }
int replace(int start, const char *search, const char *replace);
int replace(const char *search, const char *_replace) { return replace(0, search, _replace); }
// misc utilities
astring &delchr(int ch);
astring &replacechr(int ch, int newch);
astring &makeupper();
astring &makelower();
astring &trimspace();
private:
// internal helpers
astring &init();
char *safe_string_base(int start) const;
bool ensure_room(int length);
void normalize_substr(int &start, int &count, int length) const;
// internal state
char * m_text;
int m_alloclen;
char m_smallbuf[64];
int m_len;
};
#endif /* __ASTRING_H__ */
|