xref: /haiku/src/add-ons/kernel/file_systems/packagefs/util/String.h (revision 97f11716bfaa0f385eb0e28a52bf56a5023b9e99)
1d07c930cSIngo Weinhold /*
2d07c930cSIngo Weinhold  * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3d07c930cSIngo Weinhold  * Distributed under the terms of the MIT License.
4d07c930cSIngo Weinhold  */
5d07c930cSIngo Weinhold #ifndef STRING_H
6d07c930cSIngo Weinhold #define STRING_H
7d07c930cSIngo Weinhold 
8d07c930cSIngo Weinhold 
9d07c930cSIngo Weinhold #include "StringPool.h"
10d07c930cSIngo Weinhold 
11d07c930cSIngo Weinhold 
12d07c930cSIngo Weinhold class String {
13d07c930cSIngo Weinhold public:
14d07c930cSIngo Weinhold 								String();
15d07c930cSIngo Weinhold 								String(const String& other);
16d07c930cSIngo Weinhold 								~String();
17d07c930cSIngo Weinhold 
18d07c930cSIngo Weinhold 			bool				SetTo(const char* string);
19d07c930cSIngo Weinhold 			bool				SetTo(const char* string, size_t maxLength);
20d07c930cSIngo Weinhold 			bool				SetToExactLength(const char* string,
21d07c930cSIngo Weinhold 									size_t length);
22d07c930cSIngo Weinhold 
23d07c930cSIngo Weinhold 			const char*			Data() const;
24d07c930cSIngo Weinhold 			uint32				Hash() const;
25d07c930cSIngo Weinhold 
26d07c930cSIngo Weinhold 			bool				IsEmpty() const;
27d07c930cSIngo Weinhold 
28d07c930cSIngo Weinhold 			String&				operator=(const String& other);
29d07c930cSIngo Weinhold 
30d07c930cSIngo Weinhold 			bool				operator==(const String& other) const;
31d07c930cSIngo Weinhold 			bool				operator!=(const String& other) const;
32d07c930cSIngo Weinhold 
33d07c930cSIngo Weinhold 								operator const char*() const;
34d07c930cSIngo Weinhold 
35d07c930cSIngo Weinhold private:
36d07c930cSIngo Weinhold 			StringData*			fData;
37d07c930cSIngo Weinhold };
38d07c930cSIngo Weinhold 
39d07c930cSIngo Weinhold 
40d07c930cSIngo Weinhold inline
String()41d07c930cSIngo Weinhold String::String()
42d07c930cSIngo Weinhold 	:
43d07c930cSIngo Weinhold 	fData(StringData::GetEmpty())
44d07c930cSIngo Weinhold {
45d07c930cSIngo Weinhold }
46d07c930cSIngo Weinhold 
47d07c930cSIngo Weinhold 
48d07c930cSIngo Weinhold inline
String(const String & other)49d07c930cSIngo Weinhold String::String(const String& other)
50d07c930cSIngo Weinhold 	:
51d07c930cSIngo Weinhold 	fData(other.fData)
52d07c930cSIngo Weinhold {
53d07c930cSIngo Weinhold 	fData->AcquireReference();
54d07c930cSIngo Weinhold }
55d07c930cSIngo Weinhold 
56d07c930cSIngo Weinhold 
57d07c930cSIngo Weinhold inline
~String()58d07c930cSIngo Weinhold String::~String()
59d07c930cSIngo Weinhold {
60d07c930cSIngo Weinhold 	fData->ReleaseReference();
61d07c930cSIngo Weinhold }
62d07c930cSIngo Weinhold 
63d07c930cSIngo Weinhold 
64d07c930cSIngo Weinhold inline bool
SetTo(const char * string)65d07c930cSIngo Weinhold String::SetTo(const char* string)
66d07c930cSIngo Weinhold {
67*0b19ba0aSAugustin Cavalier 	return SetToExactLength(string, (string == NULL) ? 0 : strlen(string));
68d07c930cSIngo Weinhold }
69d07c930cSIngo Weinhold 
70d07c930cSIngo Weinhold 
71d07c930cSIngo Weinhold inline bool
SetTo(const char * string,size_t maxLength)72d07c930cSIngo Weinhold String::SetTo(const char* string, size_t maxLength)
73d07c930cSIngo Weinhold {
74d07c930cSIngo Weinhold 	return SetToExactLength(string, strnlen(string, maxLength));
75d07c930cSIngo Weinhold }
76d07c930cSIngo Weinhold 
77d07c930cSIngo Weinhold 
78d07c930cSIngo Weinhold inline const char*
Data()79d07c930cSIngo Weinhold String::Data() const
80d07c930cSIngo Weinhold {
81d07c930cSIngo Weinhold 	return fData->String();
82d07c930cSIngo Weinhold }
83d07c930cSIngo Weinhold 
84d07c930cSIngo Weinhold 
85d07c930cSIngo Weinhold inline uint32
Hash()86d07c930cSIngo Weinhold String::Hash() const
87d07c930cSIngo Weinhold {
88d07c930cSIngo Weinhold 	return fData->Hash();
89d07c930cSIngo Weinhold }
90d07c930cSIngo Weinhold 
91d07c930cSIngo Weinhold 
92d07c930cSIngo Weinhold inline bool
IsEmpty()93d07c930cSIngo Weinhold String::IsEmpty() const
94d07c930cSIngo Weinhold {
95d07c930cSIngo Weinhold 	return fData == StringData::Empty();
96d07c930cSIngo Weinhold }
97d07c930cSIngo Weinhold 
98d07c930cSIngo Weinhold 
99d07c930cSIngo Weinhold inline bool
100d07c930cSIngo Weinhold String::operator==(const String& other) const
101d07c930cSIngo Weinhold {
102d07c930cSIngo Weinhold 	return fData == other.fData;
103d07c930cSIngo Weinhold }
104d07c930cSIngo Weinhold 
105d07c930cSIngo Weinhold 
106d07c930cSIngo Weinhold inline bool
107d07c930cSIngo Weinhold String::operator!=(const String& other) const
108d07c930cSIngo Weinhold {
109d07c930cSIngo Weinhold 	return !(*this == other);
110d07c930cSIngo Weinhold }
111d07c930cSIngo Weinhold 
112d07c930cSIngo Weinhold 
113d07c930cSIngo Weinhold inline
114d07c930cSIngo Weinhold String::operator const char*() const
115d07c930cSIngo Weinhold {
116d07c930cSIngo Weinhold 	return fData->String();
117d07c930cSIngo Weinhold }
118d07c930cSIngo Weinhold 
119d07c930cSIngo Weinhold 
120d07c930cSIngo Weinhold #endif	// STRING_H
121