blocxx
String.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Vintela, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Vintela, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
38
39#ifndef BLOCXX_STRING_HPP_INCLUDE_GUARD_
40#define BLOCXX_STRING_HPP_INCLUDE_GUARD_
41#include "blocxx/BLOCXX_config.h"
42#include "blocxx/Types.hpp"
44#include "blocxx/CommonFwd.hpp"
45#include "blocxx/Exception.hpp"
46#include <iosfwd>
47#include <string>
48
49namespace BLOCXX_NAMESPACE
50{
51
52BLOCXX_DECLARE_APIEXCEPTION(StringConversion, BLOCXX_COMMON_API);
53
66class BLOCXX_COMMON_API String
67{
68public:
69 class ByteBuf;
73 String();
80 explicit String(Int32 val);
87 explicit String(UInt32 val);
88// do this check so we fill in the gaps and have int, long & long long constructors if necessary
89#if defined(BLOCXX_INT32_IS_INT) && defined(BLOCXX_INT64_IS_LONG_LONG)
96 explicit String(long val);
103 explicit String(unsigned long val);
104#endif
111 explicit String(Int64 val);
118 explicit String(UInt64 val);
125 explicit String(Real32 val);
132 explicit String(Real64 val);
138 String(const char* str);
145 explicit String(const Char16Array& ra) BLOCXX_DEPRECATED;
152 explicit String(Bool parm) BLOCXX_DEPRECATED;
159 explicit String(const Char16& parm) BLOCXX_DEPRECATED;
165 explicit String(const std::string& str);
166
171
182 explicit String(ETakeOwnershipFlag, char* allocatedMemory, size_t len);
191 explicit String(const char* str, size_t len);
199 String(const String& arg);
204 explicit String(char c);
208 ~String();
212 void swap(String& x);
219 char* allocateCString() const;
223 size_t length() const;
227 size_t size() const { return length(); }
233 size_t UTF8Length() const;
237 bool empty() const { return length() == 0; }
246 int format(const char* fmt, ...);
259
272 StringArray tokenize(const char* delims = " \n\r\t\v",
273 EReturnDelimitersFlag returnDelimitersAsTokens = E_DISCARD_DELIMITERS,
274 EEmptyTokenReturnFlag returnEmptyTokens = E_SKIP_EMPTY_TOKENS ) const;
279 const char* c_str() const;
284 BLOCXX_DEPRECATED const char* getBytes() const { return c_str(); }
291 char charAt(size_t ndx) const;
299 int compareTo(const String& arg) const;
307 int compareTo(const char* arg) const;
315 int compareToIgnoreCase(const String& arg) const;
323 int compareToIgnoreCase(const char* arg) const;
329 String& concat(const char* arg);
330
336 String& concat(const String& arg)
337 {
338 return concat(arg.c_str());
339 }
340
346 String& concat(char arg);
352
360 bool endsWith(const char* arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const;
361
371 bool endsWith(const String& arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const
372 {
373 return endsWith(arg.c_str(), ignoreCase);
374 }
375
382 bool endsWith(char arg) const;
383
391 bool equals(const String& arg) const;
399 bool equals(const char* arg) const;
408 bool equalsIgnoreCase(const String& arg) const;
417 bool equalsIgnoreCase(const char* arg) const;
421 UInt32 hashCode() const;
430 size_t indexOf(char ch, size_t fromIndex=0) const;
438 size_t indexOf(const char* arg, size_t fromIndex=0) const;
446 size_t indexOf(const String& arg, size_t fromIndex=0) const
447 {
448 return indexOf(arg.c_str(), fromIndex);
449 }
450
459 size_t lastIndexOf(char ch, size_t fromIndex=npos) const;
468 size_t lastIndexOf(const char* arg, size_t fromIndex=npos) const;
477 size_t lastIndexOf(const String& arg, size_t fromIndex=npos) const
478 {
479 return lastIndexOf(arg.c_str(), fromIndex);
480 }
481
490 bool startsWith(const char* arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const;
499 bool startsWith(const String& arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const
500 {
501 return startsWith(arg.c_str(), ignoreCase);
502 }
503
509 bool startsWith(char arg) const;
510
519 String substring(size_t beginIndex,
520 size_t length=npos) const;
525 bool isSpaces() const;
531 String& toLowerCase();
537 String& toUpperCase();
544 String& ltrim();
551 String& rtrim();
558 String& trim();
564 String& erase();
565
571 String& erase( size_t idx, size_t len = npos );
578 String& operator= (const String & arg);
586 const char& operator[] (size_t ndx) const;
587 char& operator[] (size_t ndx);
594 String& operator+= (const String& arg) { return concat(arg); }
601 String& operator+= (const char* arg) { return concat(arg); }
608 String& operator+= (char arg) { return concat(arg); }
615 void readObject(std::streambuf & istrm);
621 void writeObject(std::streambuf & ostrm) const;
625 String toString() const;
631 Char16 toChar16() const BLOCXX_DEPRECATED;
636 Real32 toReal32() const;
641 Real64 toReal64() const;
647 bool toBool() const;
652 UInt8 toUInt8(int base=10) const;
657 Int8 toInt8(int base=10) const;
662 UInt16 toUInt16(int base=10) const;
667 Int16 toInt16(int base=10) const;
672 UInt32 toUInt32(int base=10) const;
677 Int32 toInt32(int base=10) const;
682 UInt64 toUInt64(int base=10) const;
687 Int64 toInt64(int base=10) const;
692 unsigned int toUnsignedInt(int base=10) const;
697 int toInt(int base=10) const;
708 static unsigned long long int strtoull(const char* nptr, char** endptr,
709 int base);
720 static long long int strtoll(const char* nptr, char** endptr, int base);
728 static const char* strchr(const char* theStr, int c);
737 static String getLine(std::istream& istr);
738
739#if defined(BLOCXX_AIX)
740 static const size_t npos;
741#else
742 static const size_t npos = size_t(~0);
743#endif // BLOCXX_AIX
744
745#ifdef BLOCXX_WIN32
746#pragma warning (push)
747#pragma warning (disable: 4251)
748#endif
749
751private:
753
754#ifdef BLOCXX_WIN32
755#pragma warning (pop)
756#endif
757
758};
761
762BLOCXX_COMMON_API std::ostream& operator<< (std::ostream& ostr, const String& arg);
763BLOCXX_COMMON_API String operator + (const String& s1, const String& s2);
764BLOCXX_COMMON_API String operator + (const char* p, const String& s);
765BLOCXX_COMMON_API String operator + (const String& s, const char* p);
766BLOCXX_COMMON_API String operator + (char c, const String& s);
767BLOCXX_COMMON_API String operator + (const String& s, char c);
768inline bool
769operator == (const String& s1, const String& s2)
770{
771 return (s1.compareTo(s2) == 0);
772}
773inline bool
774operator == (const String& s, const char* p)
775{
776 return (s.compareTo(p) == 0);
777}
778inline bool
779operator == (const char* p, const String& s)
780{
781 return (s.compareTo(p) == 0);
782}
783inline bool
784operator != (const String& s1, const String& s2)
785{
786 return (s1.compareTo(s2) != 0);
787}
788inline bool
789operator != (const String& s, const char* p)
790{
791 return (s.compareTo(p) != 0);
792}
793inline bool
794operator != (const char* p, const String& s)
795{
796 return (s.compareTo(p) != 0);
797}
798inline bool
799operator < (const String& s1, const String& s2)
800{
801 return (s1.compareTo(s2) < 0);
802}
803inline bool
804operator < (const String& s, const char* p)
805{
806 return (s.compareTo(p) < 0);
807}
808inline bool
809operator < (const char* p, const String& s)
810{
811 return (String(p).compareTo(s) < 0);
812}
813inline bool
814operator <= (const String& s1, const String& s2)
815{
816 return (s1.compareTo(s2) <= 0);
817}
818inline bool
819operator <= (const String& s, const char* p)
820{
821 return (s.compareTo(p) <= 0);
822}
823inline bool
824operator <= (const char* p, const String& s)
825{
826 return (String(p).compareTo(s) <= 0);
827}
828inline bool
829operator > (const String& s1, const String& s2)
830{
831 return (s1.compareTo(s2) > 0);
832}
833inline bool
834operator > (const String& s, const char* p)
835{
836 return (s.compareTo(p) > 0);
837}
838inline bool
839operator > (const char* p, const String& s)
840{
841 return (String(p).compareTo(s) > 0);
842}
843inline bool
844operator >= (const String& s1, const String& s2)
845{
846 return (s1.compareTo(s2) >= 0);
847}
848inline bool
849operator >= (const String& s, const char* p)
850{
851 return (s.compareTo(p) >= 0);
852}
853inline bool
854operator >= (const char* p, const String& s)
855{
856 return (String(p).compareTo(s) >= 0);
857}
858
859} // end namespace BLOCXX_NAMESPACE
860
861#endif
#define BLOCXX_DECLARE_APIEXCEPTION(NAME, LINKAGE_SPEC)
Declare a new exception class named <NAME>Exception that derives from Exception This macro is typical...
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability.
Definition ArrayFwd.hpp:46
The Bool class is an abstraction for the boolean data type.
Definition Bool.hpp:57
COWIntrusiveReference A smart pointer that uses intrusive reference counting.
The Char16 class is an abstraction for a double byte character.
Definition Char16.hpp:57
This String class is an abstract data type that represents as NULL terminated string of characters.
Definition String.hpp:67
const char * c_str() const
Definition String.cpp:905
bool startsWith(const String &arg, EIgnoreCaseFlag ignoreCase=E_CASE_SENSITIVE) const
Determine if this String object starts with a given substring.
Definition String.hpp:499
String & concat(const char *arg)
Append a C string to this String object.
Definition String.cpp:442
String()
Create a new String object with a length of 0.
Definition String.cpp:139
size_t length() const
Definition String.cpp:354
bool endsWith(const String &arg, EIgnoreCaseFlag ignoreCase=E_CASE_SENSITIVE) const
Determine if this String object ends with the same string represented by another String object.
Definition String.hpp:371
String & concat(const String &arg)
Append another String object to this String object.
Definition String.hpp:336
size_t size() const
Definition String.hpp:227
size_t indexOf(const String &arg, size_t fromIndex=0) const
Find the first occurence of a string in this String object.
Definition String.hpp:446
bool startsWith(const char *arg, EIgnoreCaseFlag ignoreCase=E_CASE_SENSITIVE) const
Determine if this String object starts with a given substring.
Definition String.cpp:672
size_t lastIndexOf(const String &arg, size_t fromIndex=npos) const
Find the last occurence of a string in this String object.
Definition String.hpp:477
BLOCXX_DEPRECATED const char * getBytes() const
DEPRECATED in favor of c_str() in 3.1.0.
Definition String.hpp:284
size_t indexOf(char ch, size_t fromIndex=0) const
Find the first occurence of a given character in this String object.
Definition String.cpp:556
static const size_t npos
Definition String.hpp:742
size_t lastIndexOf(char ch, size_t fromIndex=npos) const
Find the last occurence of a character in this String object.
Definition String.cpp:603
COWIntrusiveReference< ByteBuf > buf_t
Definition String.hpp:750
int compareTo(const String &arg) const
Compare another String object with this one.
Definition String.cpp:418
bool endsWith(const char *arg, EIgnoreCaseFlag ignoreCase=E_CASE_SENSITIVE) const
Determine if this String object ends with given C string.
Definition String.cpp:487
Taken from RFC 1321.
Char16 operator+(const Char16 &arg1, const Char16 &arg2)
Definition Char16.hpp:316
unsigned char UInt8
Definition Types.hpp:66
signed char Int8
Definition Types.hpp:67
Array< Char16 > Char16Array
Definition CommonFwd.hpp:76
bool operator<(const Array< T > &x, const Array< T > &y)
bool operator<=(const Array< T > &x, const Array< T > &y)
Determine if one Array is less than or equal to another.
Definition Array.hpp:469
Array< String > StringArray
Definition CommonFwd.hpp:73
bool operator>(const Array< T > &x, const Array< T > &y)
Determine if one Array is greater than another.
Definition Array.hpp:515
bool operator==(const Array< T > &x, const Array< T > &y)
ostream & operator<<(ostream &ostrm, const Bool &arg)
Definition Bool.cpp:77
bool operator>=(const Array< T > &x, const Array< T > &y)
Determine if one Array is greater than or equal to another.
Definition Array.hpp:492
BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, Array, Bool)
bool operator!=(const Array< T > &x, const Array< T > &y)
Determine two Arrays are not equal.
Definition Array.hpp:446
void swap(Array< T > &x, Array< T > &y)