source: trunk/src/lol/core/string.h @ 2105

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

core: implement hashing functions for half, float and double.

  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 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://sam.zoy.org/projects/COPYING.WTFPL 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/core/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        do
41        {
42            Push(*str);
43        }
44        while (*str++);
45    }
46
47    inline String(String const &s)
48      : Super((Super const &)s)
49    {
50    }
51
52    inline char &operator [](int n)
53    {
54        return ((Super &)*this)[n];
55    }
56
57    inline char const &operator [](int n) const
58    {
59        return ((Super const &)*this)[n];
60    }
61
62    inline int Count() const
63    {
64        return ((Super const &)*this).Count() - 1;
65    }
66
67    inline String operator +(String const &s) const
68    {
69        String ret(*this);
70        return ret += s;
71    }
72
73    inline String& operator +=(String const &s)
74    {
75        /* Be careful, we have a trailing zero we don't want! */
76        --m_count;
77        (Super &)*this += (Super const &)s;
78        return *this;
79    }
80
81    inline String operator +(char c) const
82    {
83        String ret(*this);
84        return ret += c;
85    }
86
87    inline String& operator +=(char c)
88    {
89        ((Super &)*this).Last() = c;
90        ((Super &)*this).Push('\0');
91        return *this;
92    }
93
94    inline bool operator ==(String const &s) const
95    {
96        if (this->m_count != s.m_count)
97            return false;
98
99        for (int i = 0; i < this->m_count; ++i)
100            if ((*this)[i] != s[i])
101                return false;
102
103        return true;
104    }
105
106    inline bool operator !=(String const &s) const
107    {
108        return !(*this == s);
109    }
110
111#ifdef __GNUC__
112#   define LOL_FMT_ATTR(n, p) __attribute__((format(printf, n, p)))
113#else
114#   define LOL_FMT_ATTR(n, p)
115#endif
116    static String Printf(char const *format, ...) LOL_FMT_ATTR(1, 2);
117#undef LOL_FMT_ATTR
118};
119
120} /* namespace lol */
121
122#endif // __LOL_CORE_STRING_H__
123
Note: See TracBrowser for help on using the repository browser.