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 
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 
49 namespace BLOCXX_NAMESPACE
50 {
51 
52 BLOCXX_DECLARE_APIEXCEPTION(StringConversion, BLOCXX_COMMON_API);
53 
66 class BLOCXX_COMMON_API String
67 {
68 public:
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)
90 
96  explicit String(long val);
103  explicit String(unsigned long val);
104 #endif
105 
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 
168  {
169  E_TAKE_OWNERSHIP
170  };
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, ...);
248  {
249  E_DISCARD_TOKENS, // deprecated in 3.1.0
250  E_RETURN_TOKENS, // deprecated in 3.1.0
252  E_RETURN_DELIMITERS
253  };
255  {
257  E_RETURN_EMPTY_TOKENS
258  };
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);
348  {
350  E_CASE_INSENSITIVE
351  };
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  }
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 
751 private:
753 
754 #ifdef BLOCXX_WIN32
755 #pragma warning (pop)
756 #endif
757 
758 };
759 BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, Array, String);
760 BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, Enumeration, String);
761 
762 BLOCXX_COMMON_API std::ostream& operator<< (std::ostream& ostr, const String& arg);
763 BLOCXX_COMMON_API String operator + (const String& s1, const String& s2);
764 BLOCXX_COMMON_API String operator + (const char* p, const String& s);
765 BLOCXX_COMMON_API String operator + (const String& s, const char* p);
766 BLOCXX_COMMON_API String operator + (char c, const String& s);
767 BLOCXX_COMMON_API String operator + (const String& s, char c);
768 inline bool
769 operator == (const String& s1, const String& s2)
770 {
771  return (s1.compareTo(s2) == 0);
772 }
773 inline bool
774 operator == (const String& s, const char* p)
775 {
776  return (s.compareTo(p) == 0);
777 }
778 inline bool
779 operator == (const char* p, const String& s)
780 {
781  return (s.compareTo(p) == 0);
782 }
783 inline bool
784 operator != (const String& s1, const String& s2)
785 {
786  return (s1.compareTo(s2) != 0);
787 }
788 inline bool
789 operator != (const String& s, const char* p)
790 {
791  return (s.compareTo(p) != 0);
792 }
793 inline bool
794 operator != (const char* p, const String& s)
795 {
796  return (s.compareTo(p) != 0);
797 }
798 inline bool
799 operator < (const String& s1, const String& s2)
800 {
801  return (s1.compareTo(s2) < 0);
802 }
803 inline bool
804 operator < (const String& s, const char* p)
805 {
806  return (s.compareTo(p) < 0);
807 }
808 inline bool
809 operator < (const char* p, const String& s)
810 {
811  return (String(p).compareTo(s) < 0);
812 }
813 inline bool
814 operator <= (const String& s1, const String& s2)
815 {
816  return (s1.compareTo(s2) <= 0);
817 }
818 inline bool
819 operator <= (const String& s, const char* p)
820 {
821  return (s.compareTo(p) <= 0);
822 }
823 inline bool
824 operator <= (const char* p, const String& s)
825 {
826  return (String(p).compareTo(s) <= 0);
827 }
828 inline bool
829 operator > (const String& s1, const String& s2)
830 {
831  return (s1.compareTo(s2) > 0);
832 }
833 inline bool
834 operator > (const String& s, const char* p)
835 {
836  return (s.compareTo(p) > 0);
837 }
838 inline bool
839 operator > (const char* p, const String& s)
840 {
841  return (String(p).compareTo(s) > 0);
842 }
843 inline bool
844 operator >= (const String& s1, const String& s2)
845 {
846  return (s1.compareTo(s2) >= 0);
847 }
848 inline bool
849 operator >= (const String& s, const char* p)
850 {
851  return (s.compareTo(p) >= 0);
852 }
853 inline bool
854 operator >= (const char* p, const String& s)
855 {
856  return (String(p).compareTo(s) >= 0);
857 }
858 
859 } // end namespace BLOCXX_NAMESPACE
860 
861 #endif