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

Last change on this file since 2240 was 2240, checked in by sam, 10 years ago

string: implement String::Last() and a constructor for fixed-length strings.

  • Property svn:keywords set to Id
File size: 2.8 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
22namespace lol
23{
24
25class String : protected Array<char>
26{
27private:
28    typedef Array<char> Super;
29
30public:
31    inline String()
32      : Super()
33    {
34        Push('\0');
35    }
36
37    inline String(char const *str)
38      : Super()
39    {
40        Resize((int)strlen(str));
41        memcpy(&(*this)[0], str, Count() + 1);
42    }
43
44    inline String(char const *str, int count)
45      : Super()
46    {
47        Resize(count + 1);
48        memcpy(&(*this)[0], str, Count());
49        ((Super &)*this).Last() = '\0';
50    }
51
52    inline String(String const &s)
53      : Super((Super const &)s)
54    {
55    }
56
57    inline char &operator [](int n)
58    {
59        return ((Super &)*this)[n];
60    }
61
62    inline char const &operator [](int n) const
63    {
64        return ((Super const &)*this)[n];
65    }
66
67    inline char &Last()
68    {
69        return (*this)[Count() - 1];
70    }
71
72    inline char const &Last() const
73    {
74        return (*this)[Count() - 1];
75    }
76
77    inline int Count() const
78    {
79        return ((Super const &)*this).Count() - 1;
80    }
81
82    void Resize(int count)
83    {
84        ((Super &)*this).Resize(count + 1);
85        ((Super &)*this).Last() = '\0';
86    }
87
88    inline String operator +(String const &s) const
89    {
90        String ret(*this);
91        return ret += s;
92    }
93
94    inline String& operator +=(String const &s)
95    {
96        /* Ignore the trailing zero we don't want */
97        --m_count;
98        (Super &)*this += (Super const &)s;
99        return *this;
100    }
101
102    inline String operator +(char c) const
103    {
104        String ret(*this);
105        return ret += c;
106    }
107
108    inline String& operator +=(char c)
109    {
110        ((Super &)*this).Last() = c;
111        ((Super &)*this).Push('\0');
112        return *this;
113    }
114
115    inline bool operator ==(String const &s) const
116    {
117        if (this->m_count != s.m_count)
118            return false;
119
120        for (int i = 0; i < this->m_count; ++i)
121            if ((*this)[i] != s[i])
122                return false;
123
124        return true;
125    }
126
127    inline bool operator !=(String const &s) const
128    {
129        return !(*this == s);
130    }
131
132#ifdef __GNUC__
133#   define LOL_FMT_ATTR(n, p) __attribute__((format(printf, n, p)))
134#else
135#   define LOL_FMT_ATTR(n, p)
136#endif
137    static String Printf(char const *format, ...) LOL_FMT_ATTR(1, 2);
138#undef LOL_FMT_ATTR
139};
140
141} /* namespace lol */
142
143#endif // __LOL_CORE_STRING_H__
144
Note: See TracBrowser for help on using the repository browser.