Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_str.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_STR_H
7 #define TB_STR_H
8 
9 #include "tb_types.h"
10 #include <string.h>
11 #include <stdlib.h>
12 #include <assert.h>
13 #include <utility>
14 #include <string>
15 
16 namespace tb {
17 
20 #define TB_ALL_TO_TERMINATION 2147483647
21 
23 const char *stristr(const char *arg1, const char *arg2);
24 
26 class TBStrC
27 {
28 protected:
29  char *s;
30 public:
31  TBStrC(const char *str) : s(const_cast<char *>(str)) {}
32 
33  inline int Length() const { return (int)strlen(s); }
34  inline bool IsEmpty() const { return s[0] == 0; }
35 
36  inline int Compare(const char* str) const { return strcmp(s, str); }
37  inline bool Equals(const char* str) const { return !strcmp(s, str); }
38  const char *CStr() const { return s; }
39 
40  inline const char & operator[](int n) const { assert(n >= 0); assert(n <= Length()); return s[n]; }
41  explicit inline operator const char *() const { return s; }
42  bool operator ==(const char *b) const { return strcmp(s, b) == 0; }
43  bool operator ==(const TBStrC &b) const { return strcmp(s, b.s) == 0; }
44  bool operator !=(const TBStrC &b) const { return strcmp(s, b.s) != 0; }
45  bool operator <(const TBStrC &b) const { return strcmp(s, b.s) < 0; }
46 
47  bool operator ==(const std::string &b) const { return s == b; }
48 };
49 
62 class TBStr : public TBStrC
63 {
64 public:
65  ~TBStr();
66  TBStr();
67  TBStr(const TBStr &str);
68  TBStr(TBStr && str);
69  TBStr(const char* str);
70  TBStr(const char* str, int len);
71 
72  bool Set(TBStr str) { *this = str; return true; }
73  bool SetFormatted(const char* format, ...) TB_POST_FORMAT(2,3);
74 
75  void Clear();
76 
77  void Remove(int ofs, int len);
78 
80  bool Insert(int ofs, const char *ins, int ins_len = TB_ALL_TO_TERMINATION);
81 
83  bool Append(const char *ins, int ins_len = TB_ALL_TO_TERMINATION) {
84  return Insert((int)strlen(s), ins, ins_len);
85  }
86 
88  bool Append(const TBStr & str) {
89  return Insert((int)strlen(s), (const char *)str, str.Length());
90  }
91 
93  char *CStr() const { return s; }
94 
96  double atof() const { return ::atof(s); }
97 
99  int atoi() const { return ::atoi(s); }
100 
102  long atol() const { return ::atol(s); }
103 
105  explicit inline operator char *() const { return s; }
106 
108  inline operator std::string() const { return std::string(s); }
109 
114  explicit operator bool() const;
115 
117  TBStr & operator = (TBStr str) { std::swap(s, str.s); return *this; }
118 
121  char & operator *() { return s[0]; }
122 
125  const char & operator *() const { return s[0]; }
126 
127  friend class TBValue;
128 };
129 
130 } // namespace tb
131 
132 #endif // TB_STR_H
const char * stristr(const char *arg1, const char *arg2)
Some useful C-like functions that&#39;s missing in the standard.
Definition: tb_str.cpp:25
Simple string class that doesn&#39;t own or change the string pointer.
Definition: tb_str.h:26
bool Insert(int ofs, const char *ins, int ins_len=TB_ALL_TO_TERMINATION)
Insert a c-string to this TBStr at &#39;ofs&#39;.
Definition: tb_str.cpp:156
double atof() const
Call atof() on the string.
Definition: tb_str.h:96
bool Append(const char *ins, int ins_len=TB_ALL_TO_TERMINATION)
Append a c-string to this TBStr.
Definition: tb_str.h:83
TBStr & operator=(TBStr str)
Assignment operator.
Definition: tb_str.h:117
TBStr is a simple string class.
Definition: tb_str.h:62
long atol() const
Call atol() on the string.
Definition: tb_str.h:102
TBValue holds value of a specific type.
Definition: tb_value.h:59
char * CStr() const
Get the c-string raw (char *)
Definition: tb_str.h:93
bool Append(const TBStr &str)
Append another TBStr to this string.
Definition: tb_str.h:88
int atoi() const
Call atoi() on the string.
Definition: tb_str.h:99
char & operator*()
Pointer dereference, returns a reference to the zeroth char of the string (or the terminator of an em...
Definition: tb_str.h:121