source: trunk/src/lol/base/string.h @ 2245

Last change on this file since 2245 was 2245, checked in by sam, 7 years ago

core: add missing <cstring> include in our string.h.

  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11//
12// The String class
13// ----------------
14// A very simple String class, based on Array.
15//
16
17#if !defined __LOL_CORE_STRING_H__
18#define __LOL_CORE_STRING_H__
19
20#include <lol/base/array.h>
21
22#include <cstring>
23
24namespace lol
25{
26
27class String : protected Array<char>
28{
29private:
30    typedef Array<char> Super;
31
32public:
33    inline String()
34      : Super()
35    {
36        Push('\0');
37    }
38
39    inline String(char const *str)
40      : Super()
41    {
42        using std::memcpy;
43        Resize((int)strlen(str));
44        memcpy(&(*this)[0], str, Count() + 1);
45    }
46
47    inline String(char const *str, int count)
48      : Super()
49    {
50        using std::memcpy;
51        Resize(count + 1);
52        memcpy(&(*this)[0], str, Count());
53        ((Super &)*this).Last() = '\0';
54    }
55
56    inline String(String const &s)
57      : Super((Super const &)s)
58    {
59    }
60
61    inline char &operator [](int n)
62    {
63        return ((Super &)*this)[n];
64    }
65
66    inline char const &operator [](int n) const
67    {
68        return ((Super const &)*this)[n];
69    }
70
71    inline char &Last()
72    {
73        return (*this)[Count() - 1];
74    }
75
76    inline char const &Last() const
77    {
78        return (*this)[Count() - 1];
79    }
80
81    inline int Count() const
82    {
83        return ((Super const &)*this).Count() - 1;
84    }
85
86    void Resize(int count)
87    {
88        ((Super &)*this).Resize(count + 1);
89        ((Super &)*this).Last() = '\0';
90    }
91
92    inline String operator +(String const &s) const
93    {
94        String ret(*this);
95        return ret += s;
96    }
97
98    inline String& operator +=(String const &s)
99    {
100        /* Ignore the trailing zero we don't want */
101        --m_count;
102        (Super &)*this += (Super const &)s;
103        return *this;
104    }
105
106    inline String operator +(char c) const
107    {
108        String ret(*this);
109        return ret += c;
110    }
111
112    inline String& operator +=(char c)
113    {
114        ((Super &)*this).Last() = c;
115        ((Super &)*this).Push('\0');
116        return *this;
117    }
118
119    inline bool operator ==(String const &s) const
120    {
121        if (this->m_count != s.m_count)
122            return false;
123
124        for (int i = 0; i < this->m_count; ++i)
125            if ((*this)[i] != s[i])
126                return false;
127
128        return true;
129    }
130
131    inline bool operator !=(String const &s) const
132    {
133        return !(*this == s);
134    }
135
136#ifdef __GNUC__
137#   define LOL_FMT_ATTR(n, p) __attribute__((format(printf, n, p)))
138#else
139#   define LOL_FMT_ATTR(n, p)
140#endif
141    static String Printf(char const *format, ...) LOL_FMT_ATTR(1, 2);
142#undef LOL_FMT_ATTR
143};
144
145} /* namespace lol */
146
147#endif // __LOL_CORE_STRING_H__
148
Note: See TracBrowser for help on using the repository browser.