Loading...
Searching...
No Matches
string
// vi:set ft=cpp: -*- Mode: C++ -*-
/*
* (c) 2008-2009 Alexander Warg <warg@os.inf.tu-dresden.de>
* economic rights: Technische Universität Dresden (Germany)
*
* License: see LICENSE.spdx (in this directory or the directories above)
*/
#pragma once
#include <l4/cxx/minmax>
#include <l4/cxx/basic_ostream>
namespace cxx {
class String
{
public:
typedef char const *Index;
String(char const *s) noexcept : _start(s), _len(__builtin_strlen(s)) {}
String(char const *s, unsigned long len) noexcept : _start(s), _len(len) {}
String(char const *s, char const *e) noexcept : _start(s), _len(e - s) {}
String() : _start(0), _len(0) {}
Index start() const { return _start; }
Index end() const { return _start + _len; }
int len() const { return _len; }
void start(char const *s) { _start = s; }
void len(unsigned long len) { _len = len; }
bool empty() const { return !_len; }
String head(Index end) const
{
if (end < _start)
return String();
if (eof(end))
return *this;
return String(_start, end - _start);
}
String head(unsigned long end) const
{ return head(start() + end); }
String substr(unsigned long idx, unsigned long len = ~0UL) const
{
if (idx >= _len)
return String(end(), 0UL);
return String(_start + idx, cxx::min(len, _len - idx));
}
String substr(char const *start, unsigned long len = 0) const
{
if (start >= _start && !eof(start))
{
unsigned long nlen = _start + _len - start;
if (len != 0)
nlen = cxx::min(nlen, len);
return String(start, nlen);
}
return String(end(), 0UL);
}
template< typename F >
char const *find_match(F &&match) const
{
String::Index s = _start;
while (1)
{
if (eof(s))
return s;
if (match(*s))
return s;
++s;
}
}
char const *find(char const *c) const
{ return find(c, start()); }
char const *find(int c) const
{ return find(c, start()); }
char const *rfind(char const *c) const
{
if (!_len)
return end();
char const *p = end();
--p;
while (p >= _start)
{
if (*p == *c)
return p;
--p;
}
return end();
}
Index starts_with(cxx::String const &c) const
{
unsigned long i;
for (i = 0; i < c._len && i < _len; ++i)
if (_start[i] != c[i])
return 0;
return i == c._len ? start() + i : 0;
}
char const *find(int c, char const *s) const
{
if (s < _start)
return end();
while (1)
{
if (eof(s))
return s;
if (*s == c)
return s;
++s;
}
}
char const *find(char const *c, char const *s) const
{
if (s < _start)
return end();
while (1)
{
if (eof(s))
return s;
for (char const *x = c; *x; ++x)
if (*s == *x)
return s;
++s;
}
}
char const &operator [] (unsigned long idx) const { return _start[idx]; }
char const &operator [] (int idx) const { return _start[idx]; }
char const &operator [] (Index idx) const { return *idx; }
bool eof(char const *s) const { return s >= _start + _len || !*s; }
template<typename INT>
int from_dec(INT *v) const
{
*v = 0;
Index c;
for (c = start(); !eof(c); ++c)
{
unsigned char n;
if (*c >= '0' && *c <= '9')
n = *c - '0';
else
return c - start();
*v *= 10;
*v += n;
}
return c - start();
}
template<typename INT>
int from_hex(INT *v) const
{
*v = 0;
unsigned shift = 0;
Index c;
for (c = start(); !eof(c); ++c)
{
shift += 4;
if (shift > sizeof(INT) * 8)
return -1;
unsigned char n;
if (*c >= '0' && *c <= '9')
n = *c - '0';
else if (*c >= 'A' && *c <= 'F')
n = *c - 'A' + 10;
else if (*c >= 'a' && *c <= 'f')
n = *c - 'a' + 10;
else
return c - start();
*v <<= 4;
*v |= n;
}
return c - start();
}
bool operator == (String const &o) const
{
if (len() != o.len())
return false;
for (unsigned long i = 0; i < _len; ++i)
if (_start[i] != o._start[i])
return false;
return true;
}
bool operator != (String const &o) const
{ return ! (operator == (o)); }
private:
char const *_start;
unsigned long _len;
};
}
inline
L4::BasicOStream &operator << (L4::BasicOStream &s, cxx::String const &str)
{
s.write(str.start(), str.len());
return s;
}
basic_ostream
Basic IO stream.
cxx::String
Allocation free string class with explicit length field.
Definition string:31
cxx::String::head
String head(unsigned long end) const
Prefix of length end.
Definition string:80
cxx::String::start
Index start() const
Pointer to first character.
Definition string:54
cxx::String::end
Index end() const
Pointer to first byte behind the string.
Definition string:56
cxx::String::find
char const * find(char const *c, char const *s) const
Find character set at position.
Definition string:191
cxx::String::from_hex
int from_hex(INT *v) const
Convert hex string to integer.
Definition string:257
cxx::String::String
String(char const *s) noexcept
Initialize from a zero-terminated string.
Definition string:38
cxx::String::operator==
bool operator==(String const &o) const
Equality.
Definition string:284
cxx::String::String
String(char const *s, char const *e) noexcept
Initialize with start and end pointer.
Definition string:48
cxx::String::find
char const * find(int c, char const *s) const
Find character c starting at position s. Return end() if not found.
Definition string:165
cxx::String::len
void len(unsigned long len)
Set length.
Definition string:63
cxx::String::empty
bool empty() const
Check if the string has length zero.
Definition string:65
cxx::String::eof
bool eof(char const *s) const
Check if pointer s points behind string.
Definition string:217
cxx::String::find
char const * find(char const *c) const
Find character. Return end() if not found.
Definition string:124
cxx::String::String
String(char const *s, unsigned long len) noexcept
Initialize from a pointer to first character and a length.
Definition string:40
cxx::String::substr
String substr(unsigned long idx, unsigned long len=~0UL) const
Substring of length len starting at idx.
Definition string:84
cxx::String::find_match
char const * find_match(F &&match) const
Find matching character. match should be a function such as isspace.
Definition string:108
cxx::String::find
char const * find(int c) const
Find character. Return end() if not found.
Definition string:128
cxx::String::head
String head(Index end) const
Return prefix up to index.
Definition string:68
cxx::String::Index
char const * Index
Character index type.
Definition string:35
cxx::String::from_dec
int from_dec(INT *v) const
Convert decimal string to integer.
Definition string:228
cxx::String::operator!=
bool operator!=(String const &o) const
Inequality.
Definition string:297
cxx::String::start
void start(char const *s)
Set start.
Definition string:61
cxx::String::starts_with
Index starts_with(cxx::String const &c) const
Check if c is a prefix of string.
Definition string:155
cxx::String::rfind
char const * rfind(char const *c) const
Find right-most character. Return end() if not found.
Definition string:132
cxx::String::operator[]
char const & operator[](unsigned long idx) const
Get character at idx.
Definition string:210
cxx::String::len
int len() const
Length.
Definition string:58
cxx::String::String
String()
Zero-initialize. Create an invalid string.
Definition string:51
cxx::String::substr
String substr(char const *start, unsigned long len=0) const
Substring of length len starting at start.
Definition string:93
cxx
Our C++ library.
Definition arith:11