25#include <l4/cxx/minmax>
49 String(
char const *s) noexcept : _start(s), _len(__builtin_strlen(s)) {}
51 String(
char const *s,
unsigned long len) noexcept : _start(s), _len(
len) {}
59 String(
char const *s,
char const *e) noexcept : _start(s), _len(e - s) {}
69 int len()
const {
return _len; }
72 void start(
char const *s) { _start = s; }
76 bool empty()
const {
return !_len; }
100 return String(_start + idx, cxx::min(
len, _len - idx));
108 unsigned long nlen = _start + _len -
start;
110 nlen = cxx::min(nlen,
len);
118 template<
typename F >
135 char const *
find(
char const *c)
const
143 char const *
rfind(
char const *c)
const
148 char const *p =
end();
169 for (i = 0; i < c._len && i < _len; ++i)
170 if (_start[i] != c[i])
172 return i == c._len ?
start() + i : 0;
176 char const *
find(
int c,
char const *s)
const
202 char const *
find(
char const *c,
char const *s)
const
212 for (
char const *x = c; *x; ++x)
221 char const &
operator [] (
unsigned long idx)
const {
return _start[idx]; }
228 bool eof(
char const *s)
const {
return s >= _start + _len || !*s; }
238 template<
typename INT>
246 if (*c >=
'0' && *c <=
'9')
267 template<
typename INT>
276 if (shift >
sizeof(INT) * 8)
279 if (*c >=
'0' && *c <=
'9')
281 else if (*c >=
'A' && *c <=
'F')
283 else if (*c >=
'a' && *c <=
'f')
300 for (
unsigned long i = 0; i < _len; ++i)
301 if (_start[i] != o._start[i])
320L4::BasicOStream &operator << (L4::BasicOStream &s,
cxx::String const &str)
Allocation free string class with explicit length field.
String head(unsigned long end) const
Prefix of length end.
Index start() const
Pointer to first character.
Index end() const
Pointer to first byte behind the string.
char const * find(char const *c, char const *s) const
Find character set at position.
int from_hex(INT *v) const
Convert hex string to integer.
String(char const *s) noexcept
Initialize from a zero-terminated string.
bool operator==(String const &o) const
Equality.
String(char const *s, char const *e) noexcept
Initialize with start and end pointer.
char const * find(int c, char const *s) const
Find character c starting at position s. Return end() if not found.
void len(unsigned long len)
Set length.
bool empty() const
Check if the string has length zero.
bool eof(char const *s) const
Check if pointer s points behind string.
char const * find(char const *c) const
Find character. Return end() if not found.
String(char const *s, unsigned long len) noexcept
Initialize from a pointer to first character and a length.
String substr(unsigned long idx, unsigned long len=~0UL) const
Substring of length len starting at idx.
char const * find_match(F &&match) const
Find matching character. match should be a function such as isspace.
char const * find(int c) const
Find character. Return end() if not found.
String head(Index end) const
Return prefix up to index.
char const * Index
Character index type.
int from_dec(INT *v) const
Convert decimal string to integer.
bool operator!=(String const &o) const
Inequality.
void start(char const *s)
Set start.
Index starts_with(cxx::String const &c) const
Check if c is a prefix of string.
char const * rfind(char const *c) const
Find right-most character. Return end() if not found.
char const & operator[](unsigned long idx) const
Get character at idx.
String()
Zero-initialize. Create an invalid string.
String substr(char const *start, unsigned long len=0) const
Substring of length len starting at start.