-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.h
executable file
·87 lines (65 loc) · 3.4 KB
/
String.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*************************************************************************************************
Simple String Class
Copyright (C) 2009 Ezequiel Gastón Miravalles
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************************************/
/*************************************************************************************************
Software: Simple String Class
Author: Ezequiel Miravalles
Website: http://www.neoegm.com/tech/programming/c-cpp/simple-string-class/
License: GNU GPL v3 (attached, read above)
Last modification: 08/09/2009
*************************************************************************************************/
#pragma once
#include <iostream>
#include <cstring>
class String
{
char *m_pszString; //Allocated buffer
int m_nAllocated; //Allocated length
public:
//Construction and destruction
String() { m_pszString = NULL; m_nAllocated = 0; }
~String() { if (m_pszString) delete [] m_pszString; }
//Copy constructors
String(const char *pszString) { m_pszString = NULL; m_nAllocated = 0; operator=(pszString); }
String(const String& rsString) { m_pszString = NULL; m_nAllocated = 0; operator=(rsString); }
//Operators (assignment)
String& operator=(const char *pszString);
String& operator=(const String& rsString) { return operator=(rsString.m_pszString); }
//Operators (concatenation)
String& operator+=(const char *pszString);
String& operator+=(String& rsString) { return operator+=(rsString.m_pszString); }
String operator+(String rsString);
//Operators (comparison)
bool operator<(String sString) { return strcmp(operator const char*(), (const char*)sString) < 0; }
bool operator<=(String sString) { int nCmp = strcmp(operator const char*(), (const char*)sString); return nCmp < 0 || nCmp == 0; }
bool operator>(String sString) { return strcmp(operator const char*(), (const char*)sString) > 0; }
bool operator>=(String sString) { int nCmp = strcmp(operator const char*(), (const char*)sString); return nCmp > 0 || nCmp == 0; }
bool operator==(String sString) { return strcmp(operator const char*(), (const char*)sString) == 0; }
bool operator!=(String sString) { return !operator==(sString); }
//Operations
void Clear();
String Lower() { String sTmp = *this; sTmp.MakeLower(); return sTmp; }
String Upper() { String sTmp = *this; sTmp.MakeUpper(); return sTmp; }
//Information
int Length() { return m_pszString?strlen(m_pszString):0; }
//Cast operators
operator const char*() { return m_pszString?m_pszString:""; }
protected: //Helper functions
bool Reallocate(int nSize);
String& MakeLower();
String& MakeUpper();
};
//Output e input
std::ostream& operator<<(std::ostream& oStream, String& rsString);
std::istream& operator>>(std::istream& iStream, String& rsString);