String.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_STRING_H
00014 #define _SUPPORT_STRING_H
00015 
00021 #include <support/SupportDefs.h>
00022 #include <support/ITextStream.h>
00023 #include <support/Value.h>
00024 
00025 #include <string.h>
00026 #include <PalmTypes.h>              // wchar32_t
00027 
00028 #if _SUPPORTS_NAMESPACE
00029 namespace palmos {
00030 namespace support {
00031 #endif
00032 
00037 class SSharedBuffer;
00038 
00039 // These are #defines so they can be concatenated with raw strings.
00040 #define B_UTF8_ELLIPSIS             "\xE2\x80\xA6"
00041 #define B_UTF8_BULLET               "\xE2\x80\xA2"
00042 #define B_UTF8_SINGLE_OPEN_QUOTE    "\xE2\x80\x98"
00043 #define B_UTF8_SINGLE_CLOSE_QUOTE   "\xE2\x80\x99"
00044 #define B_UTF8_DOUBLE_OPEN_QUOTE    "\xE2\x80\x9C"
00045 #define B_UTF8_DOUBLE_CLOSE_QUOTE   "\xE2\x80\x9D"
00046 #define B_UTF8_COPYRIGHT            "\xC2\xA9"
00047 #define B_UTF8_REGISTERED           "\xC2\xAE"
00048 #define B_UTF8_TRADEMARK            "\xE2\x84\xA2"
00049 #define B_UTF8_SMILING_FACE_WHITE   "\xE2\x98\xBA"
00050 #define B_UTF8_SMILING_FACE_BLACK   "\xE2\x98\xBB"
00051 #define B_UTF8_HIROSHI              "\xE5\xBC\x98"
00052 
00053 enum {
00054         B_UTF32_ELLIPSIS            = 0x2026,
00055         B_UTF32_BULLET              = 0x2022,
00056         B_UTF32_SINGLE_OPEN_QUOTE   = 0x2018,
00057         B_UTF32_SINGLE_CLOSE_QUOTE  = 0x2019,
00058         B_UTF32_DOUBLE_OPEN_QUOTE   = 0x201C,
00059         B_UTF32_DOUBLE_CLOSE_QUOTE  = 0x201D,
00060         B_UTF32_COPYRIGHT           = 0x00A9,
00061         B_UTF32_REGISTERED          = 0x00AE,
00062         B_UTF32_TRADEMARK           = 0x2122,
00063         B_UTF32_SMILING_FACE_WHITE  = 0x263A,
00064         B_UTF32_SMILING_FACE_BLACK  = 0x263B
00065 };
00066 
00068 
00080 class SString
00081 {
00082 public:
00083     // --------------------------------------------------------------
00087                         SString();
00088     explicit            SString(const char *);
00089     explicit            SString(int32_t);
00090                         SString(const SString &);
00091                         SString(const char *, int32_t maxLength);
00092                         SString(const SSharedBuffer *buf);
00093                     
00094                         ~SString();
00095 
00097     static  status_t    SelfQC();
00098 
00100 
00101     // --------------------------------------------------------------
00105 
00107     const char          *String() const;
00108 
00110 
00111                         operator const char *() const { return String(); }
00112 
00114     int32_t             Length() const;
00115 
00117     void                MakeEmpty();
00118 
00120 
00125     static  const SString&  EmptyString();
00126 
00128     char                operator[](size_t index) const;
00129 
00131     char                ByteAt(int32_t index) const;
00132 
00134     bool                IsNumber(int32_t base = 10) const;
00135 
00137 
00138     // --------------------------------------------------------------
00142 
00143     SString             &operator=(const SString &);
00144     SString             &operator=(const char *);
00145     
00146     SString             &SetTo(const char *);
00147     SString             &SetTo(const char *, int32_t length);
00148 
00149     SString             &SetTo(const SString &from);
00150                         
00151     SString             &SetTo(const SString &, int32_t length);
00152         
00153     SString             &SetTo(char, int32_t count);
00154 
00156     SString             &Printf(const char *fmt, ...);
00157 
00158     void                Swap(SString& with);
00159 
00161 
00162     // --------------------------------------------------------------
00166 
00168 
00169     SString             &CopyInto(  SString &into, int32_t fromOffset,
00170                                     int32_t length) const;
00171 
00173     void                CopyInto(   char *into, int32_t fromOffset,
00174                                     int32_t length) const;
00175 
00177 
00178     // --------------------------------------------------------------
00182 
00183     SString             &operator+=(const SString &);
00184     SString             &operator+=(const char *);
00185     SString             &operator+=(char);
00186     
00187     SString             &Append(const SString &);
00188     SString             &Append(const char *);
00189     SString             &Append(const SString &, int32_t length);
00190     SString             &Append(const char *, int32_t length);
00191     SString             &Append(char, int32_t count);
00192 
00194 
00195     // --------------------------------------------------------------
00199 
00200     SString             &Prepend(const char *);
00201     SString             &Prepend(const SString &);
00202     SString             &Prepend(const char *, int32_t);
00203     SString             &Prepend(const SString &, int32_t);
00204     SString             &Prepend(char, int32_t count);
00205 
00207 
00208     // --------------------------------------------------------------
00212 
00213     SString             &Insert(const char *, int32_t pos);
00214     SString             &Insert(const char *, int32_t length, int32_t pos);
00215     SString             &Insert(const char *, int32_t fromOffset,
00216                                 int32_t length, int32_t pos);
00217 
00218     SString             &Insert(const SString &, int32_t pos);
00219     SString             &Insert(const SString &, int32_t length, int32_t pos);
00220     SString             &Insert(const SString &, int32_t fromOffset,
00221                                 int32_t length, int32_t pos);
00222     SString             &Insert(char, int32_t count, int32_t pos);
00223 
00225 
00226     // --------------------------------------------------------------
00230 
00231     SString             &Truncate(int32_t newLength);
00232                         
00233     SString             &Remove(int32_t from, int32_t length);
00234 
00235     SString             &RemoveFirst(const SString &);
00236     SString             &RemoveLast(const SString &);
00237     SString             &RemoveAll(const SString &);
00238 
00239     SString             &RemoveFirst(const char *);
00240     SString             &RemoveLast(const char *);
00241     SString             &RemoveAll(const char *);
00242     
00243     SString             &RemoveSet(const char *setOfCharsToRemove);
00244     
00245     SString             &Compact(const char *setOfCharsToCompact);
00246     SString             &Compact(const char *setOfCharsToCompact,
00247                                  char* replacementChar);
00248     
00249     SString             &MoveInto(SString &into, int32_t from, int32_t length);
00251     void                MoveInto(char *into, int32_t from, int32_t length);
00252 
00254 
00255     // --------------------------------------------------------------
00259 
00260     bool                operator<(const SString &) const;
00261     bool                operator<=(const SString &) const;
00262     bool                operator==(const SString &) const;
00263     bool                operator>=(const SString &) const;
00264     bool                operator>(const SString &) const;
00265     bool                operator!=(const SString &) const;
00266     
00267     bool                operator<(const char *) const;
00268     bool                operator<=(const char *) const;
00269     bool                operator==(const char *) const;
00270     bool                operator>=(const char *) const;
00271     bool                operator>(const char *) const;
00272     bool                operator!=(const char *) const;
00273 
00275 
00276     // --------------------------------------------------------------
00280 
00281     int                 Compare(const SString &) const;
00282     int                 Compare(const char *) const;
00283     int                 Compare(const SString &, int32_t n) const;
00284     int                 Compare(const char *, int32_t n) const;
00285     int                 ICompare(const SString &) const;
00286     int                 ICompare(const char *) const;
00287     int                 ICompare(const SString &, int32_t n) const;
00288     int                 ICompare(const char *, int32_t n) const;
00289 
00291 
00292     // --------------------------------------------------------------
00296 
00297     int32_t             FindFirst(const SString &) const;
00298     int32_t             FindFirst(const char *) const;
00299     int32_t             FindFirst(const SString &, int32_t fromOffset) const;
00300     int32_t             FindFirst(const char *, int32_t fromOffset) const;
00301     int32_t             FindFirst(char) const;
00302     int32_t             FindFirst(char, int32_t fromOffset) const;
00303 
00304     int32_t             FindLast(const SString &) const;
00305     int32_t             FindLast(const char *) const;
00306     int32_t             FindLast(const SString &, int32_t beforeOffset) const;
00307     int32_t             FindLast(const char *, int32_t beforeOffset) const;
00308     int32_t             FindLast(char) const;
00309     int32_t             FindLast(char, int32_t fromOffset) const;
00310 
00311     int32_t             IFindFirst(const SString &) const;
00312     int32_t             IFindFirst(const char *) const;
00313     int32_t             IFindFirst(const SString &, int32_t fromOffset) const;
00314     int32_t             IFindFirst(const char *, int32_t fromOffset) const;
00315 
00316     int32_t             IFindLast(const SString &) const;
00317     int32_t             IFindLast(const char *) const;
00318     int32_t             IFindLast(const SString &, int32_t beforeOffset) const;
00319     int32_t             IFindLast(const char *, int32_t beforeOffset) const;
00320 
00321     int32_t             FindSetFirst(const char *setOfChars);
00322     int32_t             FindSetFirst(const char *setOfChars, int32_t fromOffset);
00323 
00324     int32_t             FindSetLast(const char *setOfChars);
00325     int32_t             FindSetLast(const char *setOfChars, int32_t beforeOffset);
00326 
00328 
00329     // --------------------------------------------------------------
00333 
00334     SString             &ReplaceFirst(char replaceThis, char withThis);
00335     SString             &ReplaceLast(char replaceThis, char withThis);
00336     SString             &ReplaceAll(char replaceThis, char withThis,
00337                             int32_t fromOffset = 0);
00338     SString             &Replace(char replaceThis, char withThis,
00339                             int32_t maxReplaceCount, int32_t fromOffset = 0);
00340     SString             &ReplaceFirst(const char *replaceThis,
00341                             const char *withThis);
00342     SString             &ReplaceLast(const char *replaceThis,
00343                             const char *withThis);
00344     SString             &ReplaceAll(const char *replaceThis,
00345                             const char *withThis, int32_t fromOffset = 0);
00346     SString             &Replace(const char *replaceThis, const char *withThis,
00347                             int32_t maxReplaceCount, int32_t fromOffset = 0);
00348 
00349     SString             &IReplaceFirst(char replaceThis, char withThis);
00350     SString             &IReplaceLast(char replaceThis, char withThis);
00351     SString             &IReplaceAll(char replaceThis, char withThis,
00352                             int32_t fromOffset = 0);
00353     SString             &IReplace(char replaceThis, char withThis,
00354                             int32_t maxReplaceCount, int32_t fromOffset = 0);
00355     SString             &IReplaceFirst(const char *replaceThis,
00356                             const char *withThis);
00357     SString             &IReplaceLast(const char *replaceThis,
00358                             const char *withThis);
00359     SString             &IReplaceAll(const char *replaceThis,
00360                             const char *withThis, int32_t fromOffset = 0);
00361     SString             &IReplace(const char *replaceThis, const char *withThis,
00362                             int32_t maxReplaceCount, int32_t fromOffset = 0);
00363     
00364     SString             &ReplaceSet(const char *setOfChars, char with);
00365     SString             &ReplaceSet(const char *setOfChars, const char *with);
00366 
00368 
00369     // --------------------------------------------------------------
00374 
00376 
00382     char                *LockBuffer(int32_t maxLength);
00383 
00385 
00389     SString             &UnlockBuffer(int32_t length = -1);
00390     
00391     
00393 
00394     const   SSharedBuffer*  SharedBuffer() const;
00395         
00397     void                Pool();
00398 
00400 
00404     void                BeginBuffering();
00406     void                EndBuffering();
00407 
00409 
00410     // --------------------------------------------------------------
00415 
00417 
00420     int32_t             CountChars() const;
00421 
00423 
00428     int32_t             FirstForChar(int32_t index) const;
00429 
00431 
00434     int32_t             NextCharFor(int32_t index) const;
00435         
00437 
00439     int32_t             NextChar(int32_t index) const;
00440 
00442 
00446     int32_t             CharLength(int32_t index) const;
00447     
00449 
00453     wchar32_t           CharAt(int32_t index) const;
00454 
00456     SString             &AppendChar(wchar32_t unicode);
00457 
00459 
00460     // --------------------------------------------------------------
00464 
00466     SString             &ToLower();
00468     SString             &ToUpper();
00469 
00471     SString             &ToLower(int32_t first, int32_t length);
00473     SString             &ToUpper(int32_t first, int32_t length);
00474 
00476     SString             &Capitalize();
00477 
00479     SString             &CapitalizeEachWord();
00480 
00482 
00483     // --------------------------------------------------------------
00487 
00491     SString             &CharacterEscape(const char *original,
00492                             const char *setOfCharsToEscape, char escapeWith);
00495     SString             &CharacterEscape(const char *setOfCharsToEscape,
00496                             char escapeWith);
00497 
00500     SString             &CharacterDeescape(const char *original, char escapeChar);
00503     SString             &CharacterDeescape(char escapeChar);
00504 
00506 
00507     // --------------------------------------------------------------
00511 
00513     SString             &Trim();
00514     
00516 
00519     SString             &Mush();
00520     
00522     SString             &StripWhitespace();
00523 
00525 
00528     bool                IsOnlyWhitespace() const;
00529     
00531 
00532     // --------------------------------------------------------------
00536 
00537     SString             &operator<<(const char *);
00538     SString             &operator<<(const SString &);
00539     SString             &operator<<(char);
00540 
00541     SString             &operator<<(int);
00542     SString             &operator<<(unsigned int);
00543     SString             &operator<<(unsigned long);
00544     SString             &operator<<(long);
00545     SString             &operator<<(uint64_t);
00546     SString             &operator<<(int64_t);
00548     SString             &operator<<(float);
00549 
00551 
00552     // --------------------------------------------------------------
00556 
00558 
00561     status_t            PathSetTo(const SString &dir, const SString &leaf = B_EMPTY_STRING,
00562                             bool normalize = false);
00564 
00567     status_t            PathSetTo(const char *dir, const char *leaf = NULL,
00568                             bool normalize = false);
00570     status_t            PathNormalize();
00571     const char *        PathLeaf(void) const;
00573 
00574     status_t            PathGetParent(SString *parentPath) const;
00576 
00578     status_t            PathAppend(const SString &leaf, bool normalize = false);
00580 
00582     status_t            PathAppend(const char *leaf, bool normalize = false);
00584     status_t            PathGetRoot(SString* root) const;
00586 
00589     status_t            PathRemoveRoot(SString* root);
00590 
00592     static const char * PathDelimiter();
00593 
00595 
00596 /*----- Private or reserved ------------------------------------------------*/
00597 private:
00598     void                _Init(const char *, int32_t);
00599     void                _DoAssign(const char *, int32_t);
00600     void                _DoSetTo(const char *, int32_t);
00601     status_t            _PathSetTo(const char *dir, int32_t dirLength,
00602                         const char *leaf, int32_t leafLength, bool normalize);
00603     void                _DoAppend(const char *, int32_t);
00604     void                _DoAppend(const SString& s);
00605     char                *_GrowBy(int32_t);
00606     char                *_OpenAtBy(int32_t, int32_t);
00607     char                *_ShrinkAtBy(int32_t, int32_t);
00608     void                _DoPrepend(const char *, int32_t);
00609     void                _DoCompact(const char* set, const char* replace);
00610     void                _DoReplaceAt(int32_t offset, int32_t sourceLength, int32_t replaceLength, const char *withThis);
00611     int32_t             _FindAfter(const char *, int32_t, int32_t) const;
00612     int32_t             _IFindAfter(const char *, int32_t, int32_t) const;
00613     int32_t             _ShortFindAfter(const char *, int32_t) const;
00614     int32_t             _FindBefore(const char *, int32_t, int32_t) const;
00615     int32_t             _IFindBefore(const char *, int32_t, int32_t) const;
00616     
00617     void                _SetUsingAsCString(bool);
00618     void                _AssertNotUsingAsCString() const;
00619 
00620     SSharedBuffer*      _Edit(size_t newStrLen);
00621 
00622 protected:
00623     const char *        _privateData;
00624 };
00625 
00626 
00627 /*----- Type and STL utilities --------------------------------------*/
00628 _IMPEXP_SUPPORT void    BMoveBefore(SString* to, SString* from, size_t count);
00629 _IMPEXP_SUPPORT void    BMoveAfter(SString* to, SString* from, size_t count);
00630 inline void             BSwap(SString& v1, SString& v2);
00631 inline int32_t          BCompare(const SString& v1, const SString& v2);
00632 inline void             swap(SString& x, SString& y);
00633 
00634 /*----- Comutative compare operators --------------------------------------*/
00635 bool                    operator<(const char *, const SString &);
00636 bool                    operator<=(const char *, const SString &);
00637 bool                    operator==(const char *, const SString &);
00638 bool                    operator>(const char *, const SString &);
00639 bool                    operator>=(const char *, const SString &);
00640 bool                    operator!=(const char *, const SString &);
00641 
00642 /*----- Non-member compare for sorting, etc. ------------------------------*/
00643 int                     Compare(const SString &, const SString &);
00644 int                     ICompare(const SString &, const SString &);
00645 int                     Compare(const SString *, const SString *);
00646 int                     ICompare(const SString *, const SString *);
00647 
00648 /*----- Streaming into ITextOutput --------------------------------------------*/
00649 
00650 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const SString& string);
00651 
00654 /*-------------------------------------------------------------------------*/
00655 /*---- No user serviceable parts after this -------------------------------*/
00656 
00657 inline int32_t 
00658 SString::Length() const
00659 {
00660     // This is the length as contained in SSharedBuffer.  We are doing it
00661     // this way just to avoid having to include the header.
00662     return (*((int32_t *)_privateData - 1) >> 1) - 1;
00663 }
00664 
00665 inline const char *
00666 SString::String() const
00667 {
00668     return _privateData;
00669 }
00670 
00671 inline SString &
00672 SString::SetTo(const char *str)
00673 {
00674     return operator=(str);
00675 }
00676 
00677 inline char 
00678 SString::operator[](size_t index) const
00679 {
00680     return _privateData[index];
00681 }
00682 
00683 inline char 
00684 SString::ByteAt(int32_t index) const
00685 {
00686     if (index < 0 || index > Length())
00687         return 0;
00688     return _privateData[index];
00689 }
00690 
00691 inline SString &
00692 SString::operator+=(const SString &string)
00693 {
00694     _DoAppend(string);
00695     return *this;
00696 }
00697 
00698 inline SString &
00699 SString::Append(const SString &string)
00700 {
00701     _DoAppend(string);
00702     return *this;
00703 }
00704 
00705 inline SString &
00706 SString::Append(const char *str)
00707 {
00708     return operator+=(str);
00709 }
00710 
00711 inline bool 
00712 SString::operator==(const SString &string) const
00713 {
00714     return strcmp(String(), string.String()) == 0;
00715 }
00716 
00717 inline bool 
00718 SString::operator<(const SString &string) const
00719 {
00720     return strcmp(String(), string.String()) < 0;
00721 }
00722 
00723 inline bool 
00724 SString::operator<=(const SString &string) const
00725 {
00726     return strcmp(String(), string.String()) <= 0;
00727 }
00728 
00729 inline bool 
00730 SString::operator>=(const SString &string) const
00731 {
00732     return strcmp(String(), string.String()) >= 0;
00733 }
00734 
00735 inline bool 
00736 SString::operator>(const SString &string) const
00737 {
00738     return strcmp(String(), string.String()) > 0;
00739 }
00740 
00741 inline bool 
00742 SString::operator!=(const SString &string) const
00743 {
00744     return strcmp(String(), string.String()) != 0;
00745 }
00746 
00747 inline bool 
00748 SString::operator!=(const char *str) const
00749 {
00750     return !operator==(str);
00751 }
00752 
00753 inline void
00754 BSwap(SString& v1, SString& v2)
00755 {
00756     v1.Swap(v2);
00757 }
00758 
00759 inline int32_t
00760 BCompare(const SString& v1, const SString& v2)
00761 {
00762     return strcmp(v1.String(), v2.String());
00763 }
00764 
00765 inline void
00766 swap(SString& x, SString& y)
00767 {
00768     x.Swap(y);
00769 }
00770 
00771 inline bool 
00772 operator<(const char *str, const SString &string)
00773 {
00774     return string > str;
00775 }
00776 
00777 inline bool 
00778 operator<=(const char *str, const SString &string)
00779 {
00780     return string >= str;
00781 }
00782 
00783 inline bool 
00784 operator==(const char *str, const SString &string)
00785 {
00786     return string == str;
00787 }
00788 
00789 inline bool 
00790 operator>(const char *str, const SString &string)
00791 {
00792     return string < str;
00793 }
00794 
00795 inline bool 
00796 operator>=(const char *str, const SString &string)
00797 {
00798     return string <= str;
00799 }
00800 
00801 inline bool 
00802 operator!=(const char *str, const SString &string)
00803 {
00804     return string != str;
00805 }
00806 
00807 SValue BArrayAsValue(const SString* from, size_t count);
00808 status_t BArrayConstruct(SString* to, const SValue& value, size_t count);
00809 
00810 #if _SUPPORTS_NAMESPACE
00811 } } // namespace palmos::support
00812 #endif
00813 
00814 #endif /* _SUPPORT_STRING_H */