InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PMString.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: EricM
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //========================================================================================
23 
24 #ifndef __PMSTRING__
25 #define __PMSTRING__
26 
27 #include "K2Debugging.h"
28 #include "K2TypeTraits.h"
29 #include "PMTypes.h"
30 #include "UnicodeSavvyString.h"
31 #include <string>
32 #include <adobe/move.hpp>
33 #include <adobe/typeinfo.hpp>
34 
35 class IPMStream;
36 class PMLocaleId;
37 class WideString;
38 
39 #include "PlatformChar.h"
40 #include "Invariant.h"
41 
42 // API's with DEPRECATED_PLATFORM_BUFFER will be removed in the
43 // future.
44 #define DEPRECATED_PLATFORM_BUFFER 1
45 
53 {
54  public:
55  typedef object_type data_type;
56 
63  {
68  };
69 
70  //----------Constructors-----------------
71 
72  PMString();
73 
82  PMString(ConstCString key, TranslateDuringCall translate = kDontTranslateDuringCall);
83 
93  PMString(const UTF16TextChar * string, int32 nDblBytes);
94 
101  explicit PMString(const wchar_t* s);
102 
109  PMString(const wchar_t* s, size_t len);
110 
113  PMString(adobe::move_from<PMString> other)
114  : UnicodeSavvyString(adobe::move_from<UnicodeSavvyString>(other.source)),
115  fEncoding(other.source.fEncoding),
116  fShouldTranslate(other.source.fShouldTranslate), fHasTranslated(other.source.fHasTranslated)
117 #if defined(DEBUG)
118  ,fFormatNumber(other.source.fFormatNumber)
119 #endif
120  {
121  }
122 
123  PMString(PMString &&other) noexcept
124  : UnicodeSavvyString(std::move(other)),
125  fEncoding(other.fEncoding),
126  fShouldTranslate(other.fShouldTranslate), fHasTranslated(other.fHasTranslated)
127 #if defined(DEBUG)
128  , fFormatNumber(other.fFormatNumber)
129 #endif
130  {
131  }
132 
133  PMString(const PMString& s);
134  PMString(const WideString& s);
135  ~PMString();
136 
137 
138  //----------GET/SET STRING Routines-----------------
139 
143  void SetString(const PMString& s);
144 
151  void SetKey(ConstCString key, TranslateDuringCall translate = kDontTranslateDuringCall);
152 
153  //----------GET/SET UNICODE STRING Routines-----------------
154 
160  void GetWChar_tString(wchar_t *wchar_tString, int32 bufferSize) const;
161 
162 
169  void SetXString(const UTF16TextChar* x, int32 nDblBytes);
170 
171  // Discriminator for wchar_t size
172  template<int bytes>
173  struct wchar_size{};
174 
182  inline PMString& assign(const wchar_t* s, size_t len)
183  {
184  fShouldTranslate = kFalse;
185  fHasTranslated = kFalse;
186  return assign_impl(s, len, wchar_size<sizeof(wchar_t)>());
187  }
188 
189 #if 0// routine defined in UnicodeSavvyString.h included here for reference
190 
194  const UTF16TextChar * GrabUTF16Buffer(int32 *numUTF16s) const;
195 
199  int32 NumUTF16TextChars() const;
200 #endif
201 
206  std::string GetUTF8String() const;
207 
210  void SetUTF8String(const std::string &utf8String);
211 
212  //----------GET/SET OS STRING Routines-----------------
213 
214 #ifdef MACINTOSH
215 
218  CFStringRef CreateCFString() const;
219 
224  void SetCFString(CFStringRef cfstring);
225 #endif
226 
227 #ifdef WINDOWS
228 
231  TCHAR* GrabTString(void)
232  { return (TCHAR*)UnicodeSavvyString::GrabUTF16Buffer(nil); }
233 
238  const TCHAR* GrabTString(void) const
239  { return (const TCHAR*)UnicodeSavvyString::GrabUTF16Buffer(nil); }
240 
245  void SetTString(const TCHAR* x);
246 
250  uint32 GetTLength() const;
251 #endif
252 
253 
254  //---------CHARACTER LEVEL ROUTINES-----------------
255 
256  // NOTE: Routines with a "W" in the name imply the operation occurs on the Unicode representation
257  // of the string.
258  // Using the Unicode version is recommended.
259 
265  UTF32TextChar GetWChar(int32 pos) const;
266 
267 
268  //---------BASIC STRING ROUTINES-----------------
269 
270  // basic string routines, clear, length, etc.
271 
272 #if 0// routine defined in UnicodeSavvyString.h included here for reference
273 
278  size_type CharCount() const;
279 #endif
280 
285  int32 WCharLength(void) const;
286 
290  bool16 empty() const;
291 
295  void clear();
296 
297  //---------APPEND ROUTINES-----------------
298 
303  void Append(const PMString &s, CharCounter nCharacters = kMaxInt32);
304 
308  void Append(const WideString& s);
309 
314  void AppendW(const UTF16TextChar * ws, int32 nDblBytes = kMaxInt32);
315 
319  void AppendW(UTF32TextChar wc);
320 
326  void append(const wchar_t *s);
327 
328 
329  //---------INSERT ROUTINES-----------------
330 
338  void Insert(const PMString &s, CharCounter pos = 0, CharCounter nCharacters = kMaxInt32);
339 
345  void InsertW(const UTF16TextChar *ws, int32 nDblBytes = kMaxInt32, int32 pos = 0);
346 
351  void InsertW(UTF32TextChar wc, int32 pos = 0);
352 
353 
354  //---------PARSE ROUTINES-----------------
355 
362  PMString* Substring(CharCounter pos, CharCounter count = kMaxInt32) const;
363 
372  PMString* GetItem(const PMString& delimiter, const int32 nItem) const;
373 
379  void Remove(CharCounter pos, CharCounter count = 1);
380 
384  void Truncate(CharCounter count = 1);
385 
386 
391  typedef enum {
400  } WhitespaceType;
401 
406 
414  CharCounter IndexOfString(const PMString& keyString, CharCounter pos = 0) const;
415 
421  CharCounter LastIndexOfString(const PMString& keyString) const;
422 
428  CharCounter IndexOfWChar(UTF32TextChar wc, int32 pos = 0) const;
429 
434  CharCounter LastIndexOfWChar(UTF32TextChar wc) const;
435 
441  bool16 Contains(const PMString& searchString, CharCounter pos = 0) const;
442 
448  bool16 BeginsWith(const PMString& searchString, CharCounter pos = 0) const;
449 
455 
456 
457  //----------Compare Routines---------------
458 
465  int32 Compare(bool16 casesensitive, const PMString &s) const;
466 
474  bool16 IsEqual(const PMString &s, bool8 casesensitive = kTrue, bool8 limitCompareToShorterLength = kFalse) const;
475 
476 
477  //----------OPERATORS---------------
478 
483  bool16 operator == (const PMString &s) const;
484 
488  bool16 operator == (ConstCString key) const;
489 
493  bool16 operator != (const PMString &s) const;
494 
500  bool16 operator < (const PMString &s) const;
501 
507  bool16 operator > (const PMString &s) const;
508 
514  bool16 operator <= (const PMString &s) const;
515 
521  bool16 operator >= (const PMString &s) const;
522 
526  PMString& operator+=(const PMString& s);
527 
533  friend PMString operator+(const PMString& s1, const PMString& s2);
534 
535 
538  friend void swap( PMString& left, PMString& right ) noexcept;
539 
542  inline PMString& operator=(const PMString &other)
543  {
544  if (&other != this)
545  SetString(other);
546  return *this;
547  }
548 
551  inline PMString& operator=(PMString &&other) noexcept
552  {
553  swap(*this, other);
554  return *this;
555  }
556 
557  //---------------MISC ROUTINES------------------------
558 
562  uint32 Hash(void) const;
563 
567  void AppendNumber(int32 i);
568 
576  void AppendNumber(const PMReal& r,int32 digitsPrecision = -1, bool16 round = kFalse, bool16 eliminateTrailingZeros = kFalse);
577 
581  void AsNumber(int32 i);
582 
590  void AsNumber(const PMReal& r,int32 digitsPrecision = -1, bool16 round = kFalse, bool16 eliminateTrailingZeros = kFalse);
591 
595  void AppendFixed(Fixed f)
596  { AppendNumber( PMReal((double)(f/(double)65536.0))); }
597 
601  void AsFixed(Fixed f)
602  { AsNumber( PMReal((double)(f / (double)65536.0))); }
603 
609  typedef enum {
611  kNoError = 0,
616  } ConversionError;
622  int32 GetAsNumber( ConversionError *pError = nil, CharCounter *lenNotConverted = nil ) const;
623 
629  double GetAsDouble( ConversionError *pError = nil, CharCounter *lenNotConverted = nil ) const;
630 
633  void ToUpper(void);
634 
637  void ToLower(void);
638 
639 
640  //----------READ/WRITE ROUTINES---------------
641 
646  void ReadWrite(IPMStream *s);
647 
653  void ReadWriteKey(IPMStream *s);
654 
659  typedef enum {
678  void ReadPlatformWriteUnicode(IPMStream *s, PMString::StringPlatformEncoding encoding = kPlatformEncodingOther, bool16 includeBools = kTrue);
679 
680 
681  //----------TRANSLATE ROUTINES---------------
682 
689  bool16 Translate(const PMLocaleId& locale);
690 
695  bool16 Translate();
696 
697  //----------DISCOURAGED TRANSLATE ROUTINES---------------
698  // DISCOURAGED.
699  // As much as possible do not use these routines. If the usage does not need translation
700  // consider using WideString.
701 
707  bool16 IsTranslatable() const;
708 
720  PMString& SetTranslatable(bool16 shouldTranslate);
721 
727  bool16 HasTranslated() const;
728 
738  void SetTranslated();
739 
740 
741  //----------PLATFORM STRING Routines-----------------
742  // DISCOURAGED
743  // PMString using Unicode internally so as much as possible restrict the use of
744  // platform string calls
745 
753  {
778 
779  kUnknownEncoding = -1
780  };
781 
802  PMString(ConstCString string, PMString::StringEncoding encoding);
803 
816  void SetPString(ConstPString p, PMString::StringEncoding encoding);
817 
825  void SetCString(ConstCString C, PMString::StringEncoding encoding);
826 
835  void SetXString(const char* x, int32 nBytes, PMString::StringEncoding encoding = kUnknownEncoding);
836 
851  void GetPString(PString p, int32 bufferSize, PMString::StringEncoding encoding = kUnknownEncoding) const;
852 
860  void GetCString(CString C, int32 bufferSize, PMString::StringEncoding encoding = kUnknownEncoding) const;
861 
868  std::string GetPlatformString(PMString::StringEncoding encoding = kUnknownEncoding) const;
869 
877  void Insert(const char* ps, int32 nBytes = kMaxInt32, CharCounter pos = 0, PMString::StringEncoding encoding = kUnknownEncoding);
878 
888  void Insert(char pc, CharCounter pos = 0, PMString::StringEncoding encoding = kUnknownEncoding);
889 
894  void Insert(const PlatformChar &pc, CharCounter pos = 0);
895 
902  void Append(const char* ps, int32 nBytes = kMaxInt32, PMString::StringEncoding encoding = kUnknownEncoding);
903 
912  void Append(char pc, PMString::StringEncoding encoding = kUnknownEncoding);
913 
917  void Append(const PlatformChar &pc);
918 
922  const PlatformChar operator[](CharCounter index) const; // based on new multibyte safe GetChar()
923 
933  const PlatformChar GetChar(CharCounter pos, PMString::StringEncoding encoding = kUnknownEncoding) const;
934 
942  CharCounter IndexOfCharacter(const PlatformChar &pc, CharCounter pos = 0) const;
943 
948  CharCounter LastIndexOfCharacter(const PlatformChar &pc) const;
949 
950 
951  //----------NOT RECOMMENDED. All these APIs should not be used---------------
952  //API's at top are the least recommend. Please remove them first.
953 
954 #ifdef DEPRECATED_PLATFORM_BUFFER
955 
963  uint8 GetScript() const;
964 
973  void SetScript(uint8 script);
974 
984 
998  void SetEncoding(PMString::StringEncoding encoding);
999 
1007  const UTF16TextChar* GrabWString(void) const;
1008 
1020  void SetCString(ConstCString C);
1021 
1029  bool16 IsNull(void) const;
1030 
1038  bool16 IsEmpty() const;
1039 
1046  void Clear(void)
1047  {
1048  PMString::clear();
1049  }
1050 #endif
1051 
1052  protected:
1053 
1054  void AssignToPlatformBuffer(ConstCString src, int32 lengthInBytes);
1055 
1056 #ifdef DEBUG
1057  mutable InvariantCount fInvariant;
1058 public:
1059  void Invariant() const;
1060 #endif
1061  private:
1062  int32 CountChars() const;
1063 
1064  void SetUTF16Buffer(const char* x, int32 nBytes, PMString::StringEncoding encoding);
1065 
1066  int32 GetPlatformBuffer(char *buffer, int32 bufferSize, PMString::StringEncoding encoding) const;
1067  void ReadUnicode(IPMStream *s);
1068  void WriteUnicode(IPMStream *s);
1069 
1070  inline PMString& assign_impl(const wchar_t* s, size_t len, wchar_size<2>)
1071  {
1072  SetXString(reinterpret_cast<const UTF16TextChar*>(s), static_cast<int32>(len));
1073  return *this;
1074  }
1075 
1076  PMString& assign_impl(const wchar_t* s, size_t len, wchar_size<4>);
1077 
1078  inline PMString& append_dispatch(const wchar_t* s, size_t len)
1079  {
1080  return append_impl(s, len, wchar_size<sizeof(wchar_t)>());
1081  }
1082 
1083  inline PMString& append_impl(const wchar_t* s, size_t len, wchar_size<2>)
1084  {
1085  AppendW(reinterpret_cast<const UTF16TextChar*>(s), static_cast<int32>(len));
1086  return *this;
1087  }
1088 
1089  PMString& append_impl(const wchar_t* s, size_t len, wchar_size<4>);
1090 
1091  StringEncoding fEncoding;
1092  bool16 fShouldTranslate;
1093  bool16 fHasTranslated;
1094 
1095 public:
1096 #if defined(DEBUG)
1097  uchar fFormatNumber;
1098 #endif
1099 };
1100 
1101 extern const PMString kNullString;
1102 
1103 
1104 // Friend functions ---
1105 
1106 inline PMString operator+(const PMString& s1, const PMString& s2)
1107 {
1108  PMString result(s1);
1109  result.Append(s2);
1110  return result;
1111 }
1112 
1113 inline void swap( PMString& left, PMString& right ) noexcept
1114 {
1115  // Base class swap
1116  swap(static_cast<UnicodeSavvyString&>(left), static_cast<UnicodeSavvyString&>(right));
1117 
1118  std::swap(left.fEncoding, right.fEncoding);
1119  std::swap(left.fShouldTranslate, right.fShouldTranslate);
1120  std::swap(left.fHasTranslated, right.fHasTranslated);
1121 #if defined(DEBUG)
1122  std::swap(left.fFormatNumber, right.fFormatNumber);
1123 #endif
1124 }
1125 
1126 // specialize adobe::type_info<> to avoid typeid problems across DLL boundaries
1127 ADOBE_NAME_TYPE_0("pmstring:indesign:adobe",PMString);
1128 #endif