string.h 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: string.h
  3. // Purpose: interface of wxStringBuffer, wxString
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxString
  9. String class for passing textual data to or receiving it from wxWidgets.
  10. @note
  11. While the use of wxString is unavoidable in wxWidgets program, you are
  12. encouraged to use the standard string classes @c std::string or @c
  13. std::wstring in your applications and convert them to and from wxString
  14. only when interacting with wxWidgets.
  15. wxString is a class representing a Unicode character string but with
  16. methods taking or returning both @c wchar_t wide characters and @c wchar_t*
  17. wide strings and traditional @c char characters and @c char* strings. The
  18. dual nature of wxString API makes it simple to use in all cases and,
  19. importantly, allows the code written for either ANSI or Unicode builds of
  20. the previous wxWidgets versions to compile and work correctly with the
  21. single unified Unicode build of wxWidgets 3.0. It is also mostly
  22. transparent when using wxString with the few exceptions described below.
  23. @section string_api API overview
  24. wxString tries to be similar to both @c std::string and @c std::wstring and
  25. can mostly be used as either class. It provides practically all of the
  26. methods of these classes, which behave exactly the same as in the standard
  27. C++, and so are not documented here (please see any standard library
  28. documentation, for example http://en.cppreference.com/w/cpp/string for more
  29. details).
  30. In addition to these standard methods, wxString adds functions dealing with
  31. the conversions between different string encodings, described below, as
  32. well as many extra helpers such as functions for formatted output
  33. (Printf(), Format(), ...), case conversion (MakeUpper(), Capitalize(), ...)
  34. and various others (Trim(), StartsWith(), Matches(), ...). All of the
  35. non-standard methods follow wxWidgets "CamelCase" naming convention and are
  36. documented here.
  37. Notice that some wxString methods exist in several versions for
  38. compatibility reasons. For example all of length(), Length() and Len() are
  39. provided. In such cases it is recommended to use the standard string-like
  40. method, i.e. length() in this case.
  41. @section string_conv Converting to and from wxString
  42. wxString can be created from:
  43. - ASCII string guaranteed to contain only 7 bit characters using
  44. wxString::FromAscii().
  45. - Narrow @c char* string in the current locale encoding using implicit
  46. wxString::wxString(const char*) constructor.
  47. - Narrow @c char* string in UTF-8 encoding using wxString::FromUTF8().
  48. - Narrow @c char* string in the given encoding using
  49. wxString::wxString(const char*, const wxMBConv&) constructor passing a
  50. wxCSConv corresponding to the encoding as the second argument.
  51. - Standard @c std::string using implicit wxString::wxString(const
  52. std::string&) constructor. Notice that this constructor supposes that
  53. the string contains data in the current locale encoding, use FromUTF8()
  54. or the constructor taking wxMBConv if this is not the case.
  55. - Wide @c wchar_t* string using implicit wxString::wxString(const
  56. wchar_t*) constructor.
  57. - Standard @c std::wstring using implicit wxString::wxString(const
  58. std::wstring&) constructor.
  59. Notice that many of the constructors are implicit, meaning that you don't
  60. even need to write them at all to pass the existing string to some
  61. wxWidgets function taking a wxString.
  62. Similarly, wxString can be converted to:
  63. - ASCII string using wxString::ToAscii(). This is a potentially
  64. destructive operation as all non-ASCII string characters are replaced
  65. with a placeholder character.
  66. - String in the current locale encoding implicitly or using c_str() or
  67. mb_str() methods. This is a potentially destructive operation as an @e
  68. empty string is returned if the conversion fails.
  69. - String in UTF-8 encoding using wxString::utf8_str().
  70. - String in any given encoding using mb_str() with the appropriate
  71. wxMBConv object. This is also a potentially destructive operation.
  72. - Standard @c std::string using wxString::ToStdString(). The contents
  73. of the returned string use the current locale encoding, so this
  74. conversion is potentially destructive as well.
  75. - Wide C string using wxString::wc_str().
  76. - Standard @c std::wstring using wxString::ToStdWstring().
  77. @note If you built wxWidgets with @c wxUSE_STL set to 1, the implicit
  78. conversions to both narrow and wide C strings are disabled and replaced
  79. with implicit conversions to @c std::string and @c std::wstring.
  80. Please notice that the conversions marked as "potentially destructive"
  81. above can result in loss of data if their result is not checked, so you
  82. need to verify that converting the contents of a non-empty Unicode string
  83. to a non-UTF-8 multibyte encoding results in non-empty string. The simplest
  84. and best way to ensure that the conversion never fails is to always use
  85. UTF-8.
  86. @section string_gotchas Traps for the unwary
  87. As mentioned above, wxString tries to be compatible with both narrow and
  88. wide standard string classes and mostly does it transparently, but there
  89. are some exceptions.
  90. @subsection string_gotchas_element String element access
  91. Some problems are caused by wxString::operator[]() which returns an object
  92. of a special proxy class allowing to assign either a simple @c char or a @c
  93. wchar_t to the given index. Because of this, the return type of this
  94. operator is neither @c char nor @c wchar_t nor a reference to one of these
  95. types but wxUniCharRef which is not a primitive type and hence can't be
  96. used in the @c switch statement. So the following code does @e not compile
  97. @code
  98. wxString s(...);
  99. switch ( s[n] ) {
  100. case 'A':
  101. ...
  102. break;
  103. }
  104. @endcode
  105. and you need to use
  106. @code
  107. switch ( s[n].GetValue() ) {
  108. ...
  109. }
  110. @endcode
  111. instead. Alternatively, you can use an explicit cast:
  112. @code
  113. switch ( static_cast<char>(s[n]) ) {
  114. ...
  115. }
  116. @endcode
  117. but notice that this will result in an assert failure if the character at
  118. the given position is not representable as a single @c char in the current
  119. encoding, so you may want to cast to @c int instead if non-ASCII values can
  120. be used.
  121. Another consequence of this unusual return type arises when it is used with
  122. template deduction or C++11 @c auto keyword. Unlike with the normal
  123. references which are deduced to be of the referenced type, the deduced type
  124. for wxUniCharRef is wxUniCharRef itself. This results in potentially
  125. unexpected behaviour, for example:
  126. @code
  127. wxString s("abc");
  128. auto c = s[0];
  129. c = 'x'; // Modifies the string!
  130. wxASSERT( s == "xbc" );
  131. @endcode
  132. Due to this, either explicitly specify the variable type:
  133. @code
  134. int c = s[0];
  135. c = 'x'; // Doesn't modify the string any more.
  136. wxASSERT( s == "abc" );
  137. @endcode
  138. or explicitly convert the return value:
  139. @code
  140. auto c = s[0].GetValue();
  141. c = 'x'; // Doesn't modify the string neither.
  142. wxASSERT( s == "abc" );
  143. @endcode
  144. @subsection string_gotchas_conv Conversion to C string
  145. A different class of problems happens due to the dual nature of the return
  146. value of wxString::c_str() method, which is also used for implicit
  147. conversions. The result of calls to this method is convertible to either
  148. narrow @c char* string or wide @c wchar_t* string and so, again, has
  149. neither the former nor the latter type. Usually, the correct type will be
  150. chosen depending on how you use the result but sometimes the compiler can't
  151. choose it because of an ambiguity, e.g.:
  152. @code
  153. // Some non-wxWidgets functions existing for both narrow and wide
  154. // strings:
  155. void dump_text(const char* text); // Version (1)
  156. void dump_text(const wchar_t* text); // Version (2)
  157. wxString s(...);
  158. dump_text(s); // ERROR: ambiguity.
  159. dump_text(s.c_str()); // ERROR: still ambiguous.
  160. @endcode
  161. In this case you need to explicitly convert to the type that you need to
  162. use or use a different, non-ambiguous, conversion function (which is
  163. usually the best choice):
  164. @code
  165. dump_text(static_cast<const char*>(s)); // OK, calls (1)
  166. dump_text(static_cast<const wchar_t*>(s.c_str())); // OK, calls (2)
  167. dump_text(s.mb_str()); // OK, calls (1)
  168. dump_text(s.wc_str()); // OK, calls (2)
  169. dump_text(s.wx_str()); // OK, calls ???
  170. @endcode
  171. @subsection string_vararg Using wxString with vararg functions
  172. A special subclass of the problems arising due to the polymorphic nature of
  173. wxString::c_str() result type happens when using functions taking an
  174. arbitrary number of arguments, such as the standard @c printf(). Due to the
  175. rules of the C++ language, the types for the "variable" arguments of such
  176. functions are not specified and hence the compiler cannot convert wxString
  177. objects, or the objects returned by wxString::c_str(), to these unknown
  178. types automatically. Hence neither wxString objects nor the results of most
  179. of the conversion functions can be passed as vararg arguments:
  180. @code
  181. // ALL EXAMPLES HERE DO NOT WORK, DO NOT USE THEM!
  182. printf("Don't do this: %s", s);
  183. printf("Don't do that: %s", s.c_str());
  184. printf("Nor even this: %s", s.mb_str());
  185. wprintf("And even not always this: %s", s.wc_str());
  186. @endcode
  187. Instead you need to either explicitly cast to the needed type:
  188. @code
  189. // These examples work but are not the best solution, see below.
  190. printf("You can do this: %s", static_cast<const char*>(s));
  191. printf("Or this: %s", static_cast<const char*>(s.c_str()));
  192. printf("And this: %s", static_cast<const char*>(s.mb_str()));
  193. wprintf("Or this: %s", static_cast<const wchar_t*>(s.wc_str()));
  194. @endcode
  195. But a better solution is to use wxWidgets-provided functions, if possible,
  196. as is the case for @c printf family of functions:
  197. @code
  198. // This is the recommended way.
  199. wxPrintf("You can do just this: %s", s);
  200. wxPrintf("And this (but it is redundant): %s", s.c_str());
  201. wxPrintf("And this (not using Unicode): %s", s.mb_str());
  202. wxPrintf("And this (always Unicode): %s", s.wc_str());
  203. @endcode
  204. Notice that wxPrintf() replaces both @c printf() and @c wprintf() and
  205. accepts wxString objects, results of c_str() calls but also @c char* and
  206. @c wchar_t* strings directly.
  207. wxWidgets provides wx-prefixed equivalents to all the standard vararg
  208. functions and a few more, notably wxString::Format(), wxLogMessage(),
  209. wxLogError() and other log functions. But if you can't use one of those
  210. functions and need to pass wxString objects to non-wx vararg functions, you
  211. need to use the explicit casts as explained above.
  212. @section string_performance Performance characteristics
  213. wxString uses @c std::basic_string internally to store its content (unless
  214. this is not supported by the compiler or disabled specifically when
  215. building wxWidgets) and it therefore inherits many features from @c
  216. std::basic_string. In particular, most modern implementations of @c
  217. std::basic_string are thread-safe and don't use reference counting (making
  218. copying large strings potentially expensive) and so wxString has the same
  219. characteristics.
  220. By default, wxString uses @c std::basic_string specialized for the
  221. platform-dependent @c wchar_t type, meaning that it is not memory-efficient
  222. for ASCII strings, especially under Unix platforms where every ASCII
  223. character, normally fitting in a byte, is represented by a 4 byte @c
  224. wchar_t.
  225. It is possible to build wxWidgets with @c wxUSE_UNICODE_UTF8 set to 1 in
  226. which case an UTF-8-encoded string representation is stored in @c
  227. std::basic_string specialized for @c char, i.e. the usual @c std::string.
  228. In this case the memory efficiency problem mentioned above doesn't arise
  229. but run-time performance of many wxString methods changes dramatically, in
  230. particular accessing the N-th character of the string becomes an operation
  231. taking O(N) time instead of O(1), i.e. constant, time by default. Thus, if
  232. you do use this so called UTF-8 build, you should avoid using indices to
  233. access the strings whenever possible and use the iterators instead. As an
  234. example, traversing the string using iterators is an O(N), where N is the
  235. string length, operation in both the normal ("wchar_t") and UTF-8 builds
  236. but doing it using indices becomes O(N^2) in UTF-8 case meaning that simply
  237. checking every character of a reasonably long (e.g. a couple of millions
  238. elements) string can take an unreasonably long time.
  239. However, if you do use iterators, UTF-8 build can be a better choice than
  240. the default build, especially for the memory-constrained embedded systems.
  241. Notice also that GTK+ and DirectFB use UTF-8 internally, so using this
  242. build not only saves memory for ASCII strings but also avoids conversions
  243. between wxWidgets and the underlying toolkit.
  244. @section string_index Index of the member groups
  245. Links for quick access to the various categories of wxString functions:
  246. - @ref_member_group{ctor, Constructors and assignment operators}
  247. - @ref_member_group{length, Length functions}
  248. - @ref_member_group{ch_access, Character access functions}
  249. - @ref_member_group{conv, Conversions functions}
  250. - @ref_member_group{concat, Concatenation functions}
  251. - @ref_member_group{cmp, Comparison functions}
  252. - @ref_member_group{substring, Substring extraction functions}
  253. - @ref_member_group{caseconv, Case conversion functions}
  254. - @ref_member_group{search, Searching and replacing functions}
  255. - @ref_member_group{numconv, Conversion to numbers functions}
  256. - @ref_member_group{fmt, Formatting and printing functions}
  257. - @ref_member_group{mem, Memory management functions}
  258. - @ref_member_group{misc, Miscellaneous functions}
  259. - @ref_member_group{iter, Iterator interface functions}
  260. - @ref_member_group{stl, STL interface functions}
  261. @library{wxbase}
  262. @category{data}
  263. @stdobjects
  264. ::wxEmptyString
  265. @see @ref overview_string, @ref overview_unicode,
  266. @ref group_funcmacro_string "String-related functions", wxUString,
  267. wxCharBuffer, wxUniChar, wxStringTokenizer, wxStringBuffer, wxStringBufferLength
  268. */
  269. class wxString
  270. {
  271. public:
  272. /**
  273. @name Standard types
  274. Types used with wxString.
  275. */
  276. //@{
  277. typedef wxUniChar value_type;
  278. typedef wxUniChar char_type;
  279. typedef wxUniCharRef reference;
  280. typedef wxChar* pointer;
  281. typedef const wxChar* const_pointer;
  282. typedef size_t size_type;
  283. typedef wxUniChar const_reference;
  284. //@}
  285. /**
  286. @member_group_name{ctor, Constructors and assignment operators}
  287. A string may be constructed either from a C string, (some number of copies of)
  288. a single character or a wide (Unicode) string. For all constructors (except the
  289. default which creates an empty string) there is also a corresponding assignment
  290. operator.
  291. See also the assign() STL-like function.
  292. */
  293. //@{
  294. /**
  295. Default constructor
  296. */
  297. wxString();
  298. /**
  299. Creates a string from another string.
  300. Just increases the ref count by 1.
  301. */
  302. wxString(const wxString& stringSrc);
  303. /**
  304. Construct a string consisting of @a nRepeat copies of ch.
  305. */
  306. wxString(wxUniChar ch, size_t nRepeat = 1);
  307. /**
  308. Construct a string consisting of @a nRepeat copies of ch.
  309. */
  310. wxString(wxUniCharRef ch, size_t nRepeat = 1);
  311. /**
  312. Construct a string consisting of @a nRepeat copies of ch
  313. converted to Unicode using the current locale encoding.
  314. */
  315. wxString(char ch, size_t nRepeat = 1);
  316. /**
  317. Construct a string consisting of @a nRepeat copies of ch.
  318. */
  319. wxString(wchar_t ch, size_t nRepeat = 1);
  320. /**
  321. Constructs a string from the string literal @a psz using
  322. the current locale encoding to convert it to Unicode (wxConvLibc).
  323. */
  324. wxString(const char *psz);
  325. /**
  326. Constructs a string from the string literal @a psz using
  327. @a conv to convert it Unicode.
  328. */
  329. wxString(const char *psz, const wxMBConv& conv);
  330. /**
  331. Constructs a string from the first @a nLength character of the string literal @a psz using
  332. the current locale encoding to convert it to Unicode (wxConvLibc).
  333. */
  334. wxString(const char *psz, size_t nLength);
  335. /**
  336. Constructs a string from the first @a nLength character of the string literal @a psz using
  337. @a conv to convert it Unicode.
  338. */
  339. wxString(const char *psz, const wxMBConv& conv, size_t nLength);
  340. /**
  341. Constructs a string from the string literal @a pwz.
  342. */
  343. wxString(const wchar_t *pwz);
  344. /**
  345. Constructs a string from the first @a nLength characters of the string literal @a pwz.
  346. */
  347. wxString(const wchar_t *pwz, size_t nLength);
  348. /**
  349. Constructs a string from @a buf using the using the current locale
  350. encoding to convert it to Unicode.
  351. */
  352. wxString(const wxCharBuffer& buf);
  353. /**
  354. Constructs a string from @a buf.
  355. */
  356. wxString(const wxWCharBuffer& buf);
  357. /**
  358. Constructs a string from @a str using the using the current locale encoding
  359. to convert it to Unicode (wxConvLibc).
  360. @see ToStdString()
  361. */
  362. wxString(const std::string& str);
  363. /**
  364. Constructs a string from @a str.
  365. @see ToStdWstring()
  366. */
  367. wxString(const std::wstring& str);
  368. /**
  369. String destructor.
  370. Note that this is not virtual, so wxString must not be inherited from.
  371. */
  372. ~wxString();
  373. /**
  374. Assignment: see the relative wxString constructor.
  375. */
  376. wxString operator =(const wxString& str);
  377. /**
  378. Assignment: see the relative wxString constructor.
  379. */
  380. wxString operator =(wxUniChar c);
  381. //@}
  382. /**
  383. @member_group_name{length, String length}
  384. These functions return the string length and/or check whether the string
  385. is empty.
  386. See also the length(), size() or empty() STL-like functions.
  387. */
  388. //@{
  389. /**
  390. Returns the length of the string.
  391. */
  392. size_t Len() const;
  393. /**
  394. Returns the length of the string (same as Len).
  395. This is a wxWidgets 1.xx compatibility function; you should not use it in new
  396. code.
  397. */
  398. size_t Length() const;
  399. /**
  400. Returns @true if the string is empty.
  401. */
  402. bool IsEmpty() const;
  403. /**
  404. Returns @true if the string is empty (same as wxString::IsEmpty).
  405. This is a wxWidgets 1.xx compatibility function; you should not use it in new
  406. code.
  407. */
  408. bool IsNull() const;
  409. /**
  410. Empty string is @false, so !string will only return @true if the
  411. string is empty.
  412. @see IsEmpty().
  413. */
  414. bool operator!() const;
  415. //@}
  416. /**
  417. @member_group_name{ch_access, Character access}
  418. Many functions below take a character index in the string.
  419. As with C strings and arrays, the indices start from 0, so the first character
  420. of a string is string[0]. An attempt to access a character beyond the end of the
  421. string (which may even be 0 if the string is empty) will provoke an assert
  422. failure in @ref overview_debugging "debug builds", but no checks are
  423. done in release builds.
  424. */
  425. //@{
  426. /**
  427. Returns the character at position @a n (read-only).
  428. */
  429. wxUniChar GetChar(size_t n) const;
  430. /**
  431. wxWidgets compatibility conversion. Same as c_str().
  432. */
  433. const wxCStrData GetData() const;
  434. /**
  435. Returns a reference to the character at position @a n.
  436. */
  437. wxUniCharRef GetWritableChar(size_t n);
  438. /**
  439. Returns a writable buffer of at least @a len bytes.
  440. It returns a pointer to a new memory block, and the existing data will not be copied.
  441. Call UngetWriteBuf() as soon as possible to put the string back into a reasonable state.
  442. This method is deprecated, please use wxStringBuffer or wxStringBufferLength instead.
  443. */
  444. wxStringCharType* GetWriteBuf(size_t len);
  445. /**
  446. Puts the string back into a reasonable state (in which it can be used
  447. normally), after GetWriteBuf() was called.
  448. The version of the function without the @a len parameter will calculate the
  449. new string length itself assuming that the string is terminated by the first
  450. @c NUL character in it while the second one will use the specified length
  451. and thus is the only version which should be used with the strings with
  452. embedded @c NULs (it is also slightly more efficient as @c strlen()
  453. doesn't have to be called).
  454. This method is deprecated, please use wxStringBuffer or wxStringBufferLength instead.
  455. */
  456. void UngetWriteBuf();
  457. /**
  458. @overload
  459. */
  460. void UngetWriteBuf(size_t len);
  461. /**
  462. Sets the character at position @e n.
  463. */
  464. void SetChar(size_t n, wxUniChar ch);
  465. /**
  466. Returns the last character.
  467. This is a wxWidgets 1.xx compatibility function;
  468. you should not use it in new code.
  469. */
  470. wxUniChar Last() const;
  471. /**
  472. Returns a reference to the last character (writable).
  473. This is a wxWidgets 1.xx compatibility function;
  474. you should not use it in new code.
  475. */
  476. wxUniCharRef Last();
  477. /**
  478. Returns the @a i-th character of the string.
  479. */
  480. wxUniChar operator [](size_t i) const;
  481. /**
  482. Returns a writable reference to the @a i-th character of the string.
  483. */
  484. wxUniCharRef operator [](size_t i);
  485. //@}
  486. /**
  487. @member_group_name{conv, Conversions}
  488. This section contains both implicit and explicit conversions to C style
  489. strings. Although implicit conversion is quite convenient, you are advised
  490. to use wc_str() for the sake of clarity.
  491. */
  492. //@{
  493. /**
  494. Returns a lightweight intermediate class which is in turn implicitly
  495. convertible to both @c const @c char* and to @c const @c wchar_t*.
  496. Given this ambiguity it is mostly better to use wc_str(), mb_str() or
  497. utf8_str() instead.
  498. Please see the @ref overview_unicode for more information about it.
  499. Note that the returned value is not convertible to @c char* or
  500. @c wchar_t*, use char_str() or wchar_str() if you need to pass
  501. string value to a function expecting non-const pointer.
  502. @see wc_str(), utf8_str(), c_str(), mb_str(), fn_str()
  503. */
  504. wxCStrData c_str() const;
  505. /**
  506. Returns an object with string data that is implicitly convertible to
  507. @c char* pointer. Note that any change to the returned buffer is lost and so
  508. this function is only usable for passing strings to legacy libraries that
  509. don't have const-correct API. Use wxStringBuffer if you want to modify
  510. the string.
  511. @see c_str()
  512. */
  513. wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const;
  514. /**
  515. Returns buffer of the specified type containing the string data.
  516. This method is only useful in template code, otherwise you should
  517. directly call mb_str() or wc_str() if you need to retrieve a narrow or
  518. wide string from this wxString. The template parameter @a t should be
  519. either @c char or @c wchar_t.
  520. Notice that retrieving a char buffer in UTF-8 build will return the
  521. internal string representation in UTF-8 while in wchar_t build the char
  522. buffer will contain the conversion of the string to the encoding of the
  523. current locale (and so can fail).
  524. @param len
  525. If non-@NULL, filled with the length of the returned buffer.
  526. @return
  527. buffer containing the string contents in the specified type,
  528. notice that it may be @NULL if the conversion failed (e.g. Unicode
  529. string couldn't be converted to the current encoding when @a T is
  530. @c char).
  531. */
  532. template <typename T>
  533. wxCharTypeBuffer<T> tchar_str(size_t *len = NULL) const;
  534. /**
  535. Returns a string representation suitable for passing to OS' functions
  536. for file handling.
  537. */
  538. const wchar_t* fn_str() const;
  539. /**
  540. @overload
  541. */
  542. const char* fn_str() const;
  543. /**
  544. @overload
  545. */
  546. const wxCharBuffer fn_str() const;
  547. /**
  548. Returns the multibyte (C string) representation of the string
  549. using @e conv's wxMBConv::cWC2MB method and returns wxCharBuffer.
  550. @see wc_str(), utf8_str(), c_str(), wxMBConv
  551. */
  552. const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
  553. /**
  554. Converts the strings contents to UTF-8 and returns it either as a
  555. temporary wxCharBuffer object or as a pointer to the internal
  556. string contents in UTF-8 build.
  557. @see wc_str(), c_str(), mb_str()
  558. */
  559. const wxScopedCharBuffer utf8_str() const;
  560. /**
  561. Converts the strings contents to the wide character representation
  562. and returns it as a temporary wxWCharBuffer object (Unix and OS X)
  563. or returns a pointer to the internal string contents in wide character
  564. mode (Windows).
  565. The macro wxWX2WCbuf is defined as the correct return type (without const).
  566. @see utf8_str(), c_str(), mb_str(), fn_str(), wchar_str()
  567. */
  568. const wchar_t* wc_str() const;
  569. /**
  570. @overload
  571. */
  572. const wxWCharBuffer wc_str() const;
  573. /**
  574. Returns an object with string data that is implicitly convertible to
  575. @c char* pointer. Note that changes to the returned buffer may or may
  576. not be lost (depending on the build) and so this function is only usable for
  577. passing strings to legacy libraries that don't have const-correct API. Use
  578. wxStringBuffer if you want to modify the string.
  579. @see mb_str(), wc_str(), fn_str(), c_str(), char_str()
  580. */
  581. wxWritableWCharBuffer wchar_str() const;
  582. /**
  583. Explicit conversion to C string in the internal representation (either
  584. wchar_t* or UTF-8-encoded char*, depending on the build).
  585. */
  586. const wxStringCharType *wx_str() const;
  587. /**
  588. Converts the string to an 8-bit string in ISO-8859-1 encoding in the
  589. form of a wxCharBuffer (Unicode builds only).
  590. This is a convenience method useful when storing binary data in
  591. wxString. It should be used @em only for this purpose. It is only valid
  592. to call this method on strings created using From8BitData().
  593. @since 2.8.4
  594. @see wxString::From8BitData()
  595. */
  596. const wxScopedCharBuffer To8BitData() const;
  597. /**
  598. Converts the string to an ASCII, 7-bit string in the form of
  599. a wxCharBuffer (Unicode builds only) or a C string (ANSI builds).
  600. Note that this conversion is only lossless if the string contains only
  601. ASCII characters as all the non-ASCII ones are replaced with the @c '_'
  602. (underscore) character.
  603. Use mb_str() or utf8_str() to convert to other encodings.
  604. */
  605. const char* ToAscii() const;
  606. /**
  607. @overload
  608. */
  609. const wxCharBuffer ToAscii() const;
  610. /**
  611. Return the string as an std::string in current locale encoding.
  612. Note that if the conversion of (Unicode) string contents to the current
  613. locale fails, the return string will be empty. Be sure to check for
  614. this to avoid silent data loss.
  615. Instead of using this function it's also possible to write
  616. @code
  617. std::string s;
  618. wxString wxs;
  619. ...
  620. s = std::string(wxs);
  621. @endcode
  622. but using ToStdString() may make the code more clear.
  623. @since 2.9.1
  624. */
  625. std::string ToStdString() const;
  626. /**
  627. Return the string as an std::wstring.
  628. Unlike ToStdString(), there is no danger of data loss when using this
  629. function.
  630. @since 2.9.1
  631. */
  632. std::wstring ToStdWstring() const;
  633. /**
  634. Same as utf8_str().
  635. */
  636. const wxScopedCharBuffer ToUTF8() const;
  637. //@}
  638. /**
  639. @member_group_name{concat, Concatenation}
  640. Almost anything may be concatenated (appended to) with a string!
  641. Note that the various operator<<() overloads work as C++ stream insertion
  642. operators. They insert the given value into the string.
  643. Precision and format cannot be set using them. Use Printf() instead.
  644. See also the insert() and append() STL-like functions.
  645. */
  646. //@{
  647. /**
  648. Appends the string literal @a psz.
  649. */
  650. wxString& Append(const char* psz);
  651. /**
  652. Appends the wide string literal @a pwz.
  653. */
  654. wxString& Append(const wchar_t* pwz);
  655. /**
  656. Appends the string literal @a psz with max length @a nLen.
  657. */
  658. wxString& Append(const char* psz, size_t nLen);
  659. /**
  660. Appends the wide string literal @a psz with max length @a nLen.
  661. */
  662. wxString& Append(const wchar_t* pwz, size_t nLen);
  663. /**
  664. Appends the string @a s.
  665. */
  666. wxString& Append(const wxString& s);
  667. /**
  668. Appends the character @a ch @a count times.
  669. */
  670. wxString &Append(wxUniChar ch, size_t count = 1u);
  671. /**
  672. Prepends @a str to this string, returning a reference to this string.
  673. */
  674. wxString& Prepend(const wxString& str);
  675. /**
  676. Concatenation: returns a new string equal to the concatenation of the operands.
  677. */
  678. wxString operator +(const wxString& x, const wxString& y);
  679. /**
  680. @overload
  681. */
  682. wxString operator +(const wxString& x, wxUniChar y);
  683. wxString& operator<<(const wxString& s);
  684. wxString& operator<<(const char* psz);
  685. wxString& operator<<(const wchar_t* pwz);
  686. wxString& operator<<(const wxCStrData& psz);
  687. wxString& operator<<(char ch);
  688. wxString& operator<<(unsigned char ch);
  689. wxString& operator<<(wchar_t ch);
  690. wxString& operator<<(const wxCharBuffer& s);
  691. wxString& operator<<(const wxWCharBuffer& s);
  692. wxString& operator<<(wxUniChar ch);
  693. wxString& operator<<(wxUniCharRef ch);
  694. wxString& operator<<(unsigned int ui);
  695. wxString& operator<<(long l);
  696. wxString& operator<<(unsigned long ul);
  697. wxString& operator<<(wxLongLong_t ll);
  698. wxString& operator<<(wxULongLong_t ul);
  699. wxString& operator<<(float f);
  700. wxString& operator<<(double d);
  701. /**
  702. Concatenation in place: the argument is appended to the string.
  703. */
  704. void operator +=(const wxString& str);
  705. /**
  706. @overload
  707. */
  708. void operator +=(wxUniChar c);
  709. //@}
  710. /**
  711. @member_group_name{cmp, Comparison}
  712. The default comparison function Cmp() is case-sensitive and so is the default
  713. version of IsSameAs(). For case insensitive comparisons you should use CmpNoCase()
  714. or give a second parameter to IsSameAs(). This last function is maybe more
  715. convenient if only equality of the strings matters because it returns a boolean
  716. @true value if the strings are the same and not 0 (which is usually @false
  717. in C) as Cmp() does.
  718. Matches() is a poor man's regular expression matcher: it only understands
  719. '*' and '?' metacharacters in the sense of DOS command line interpreter.
  720. StartsWith() is helpful when parsing a line of text which should start
  721. with some predefined prefix and is more efficient than doing direct string
  722. comparison as you would also have to precalculate the length of the prefix.
  723. See also the compare() STL-like function.
  724. */
  725. //@{
  726. /**
  727. Case-sensitive comparison.
  728. Returns a positive value if the string is greater than the argument,
  729. zero if it is equal to it or a negative value if it is less than the
  730. argument (same semantics as the standard @c strcmp() function).
  731. @see CmpNoCase(), IsSameAs().
  732. */
  733. int Cmp(const wxString& s) const;
  734. /**
  735. Case-insensitive comparison.
  736. Returns a positive value if the string is greater than the argument,
  737. zero if it is equal to it or a negative value if it is less than the
  738. argument (same semantics as the standard @c strcmp() function).
  739. @see Cmp(), IsSameAs().
  740. */
  741. int CmpNoCase(const wxString& s) const;
  742. /**
  743. Test whether the string is equal to another string @a s.
  744. The test is case-sensitive if @a caseSensitive is @true (default) or not if it is
  745. @false.
  746. @return @true if the string is equal to the other one, @false otherwise.
  747. @see Cmp(), CmpNoCase()
  748. */
  749. bool IsSameAs(const wxString& s, bool caseSensitive = true) const;
  750. /**
  751. Test whether the string is equal to the single character @a ch.
  752. The test is case-sensitive if @a caseSensitive is @true (default) or not if it is
  753. @false.
  754. @return @true if the string is equal to this character, @false otherwise.
  755. @see Cmp(), CmpNoCase()
  756. */
  757. bool IsSameAs(wxUniChar ch, bool caseSensitive = true) const;
  758. /**
  759. Returns @true if the string contents matches a mask containing '*' and '?'.
  760. */
  761. bool Matches(const wxString& mask) const;
  762. /**
  763. This function can be used to test if the string starts with the specified
  764. @a prefix.
  765. If it does, the function will return @true and put the rest of the string
  766. (i.e. after the prefix) into @a rest string if it is not @NULL.
  767. Otherwise, the function returns @false and doesn't modify the @a rest.
  768. */
  769. bool StartsWith(const wxString& prefix, wxString *rest = NULL) const;
  770. /**
  771. This function can be used to test if the string ends with the specified
  772. @e suffix. If it does, the function will return @true and put the
  773. beginning of the string before the suffix into @e rest string if it is not
  774. @NULL. Otherwise, the function returns @false and doesn't
  775. modify the @e rest.
  776. */
  777. bool EndsWith(const wxString& suffix, wxString *rest = NULL) const;
  778. //@}
  779. /**
  780. @member_group_name{substring, Substring extraction}
  781. These functions allow you to extract a substring from the string. The
  782. original string is not modified and the function returns the extracted
  783. substring.
  784. See also the at() and the substr() STL-like functions.
  785. */
  786. /**
  787. Returns a substring starting at @e first, with length @e count, or the rest of
  788. the string if @a count is the default value.
  789. */
  790. wxString Mid(size_t first, size_t nCount = wxString::npos) const;
  791. /**
  792. Returns the part of the string between the indices @a from and @a to
  793. inclusive.
  794. This is a wxWidgets 1.xx compatibility function, use Mid()
  795. instead (but note that parameters have different meaning).
  796. */
  797. wxString SubString(size_t from, size_t to) const;
  798. /**
  799. Same as Mid() (substring extraction).
  800. */
  801. wxString operator()(size_t start, size_t len) const;
  802. /**
  803. Returns the first @a count characters of the string.
  804. */
  805. wxString Left(size_t count) const;
  806. /**
  807. Returns the last @a count characters.
  808. */
  809. wxString Right(size_t count) const;
  810. /**
  811. Gets all the characters after the first occurrence of @e ch.
  812. Returns the empty string if @e ch is not found.
  813. */
  814. wxString AfterFirst(wxUniChar ch) const;
  815. /**
  816. Gets all the characters after the last occurrence of @e ch.
  817. Returns the whole string if @e ch is not found.
  818. */
  819. wxString AfterLast(wxUniChar ch) const;
  820. /**
  821. Gets all characters before the first occurrence of @e ch.
  822. Returns the whole string if @a ch is not found.
  823. @param ch The character to look for.
  824. @param rest Filled with the part of the string following the first
  825. occurrence of @a ch or cleared if it was not found. The same string
  826. is returned by AfterFirst() but it is more efficient to use this
  827. output parameter if both the "before" and "after" parts are needed
  828. than calling both functions one after the other. This parameter is
  829. available in wxWidgets version 2.9.2 and later only.
  830. @return Part of the string before the first occurrence of @a ch.
  831. */
  832. wxString BeforeFirst(wxUniChar ch, wxString *rest = NULL) const;
  833. /**
  834. Gets all characters before the last occurrence of @e ch.
  835. Returns the empty string if @a ch is not found.
  836. @param ch The character to look for.
  837. @param rest Filled with the part of the string following the last
  838. occurrence of @a ch or the copy of this string if it was not found.
  839. The same string is returned by AfterLast() but it is more efficient
  840. to use this output parameter if both the "before" and "after" parts
  841. are needed than calling both functions one after the other. This
  842. parameter is available in wxWidgets version 2.9.2 and later only.
  843. @return Part of the string before the last occurrence of @a ch.
  844. */
  845. wxString BeforeLast(wxUniChar ch, wxString *rest = NULL) const;
  846. //@}
  847. /**
  848. @member_group_name{caseconv, Case conversion}
  849. The MakeXXX() variants modify the string in place, while the other functions
  850. return a new string which contains the original text converted to the upper or
  851. lower case and leave the original string unchanged.
  852. */
  853. //@{
  854. /**
  855. Return the copy of the string with the first string character in the
  856. upper case and the subsequent ones in the lower case.
  857. @since 2.9.0
  858. @see MakeCapitalized()
  859. */
  860. wxString Capitalize() const;
  861. /**
  862. Returns this string converted to the lower case.
  863. @see MakeLower()
  864. */
  865. wxString Lower() const;
  866. /**
  867. Same as MakeLower.
  868. This is a wxWidgets 1.xx compatibility function; you should not use it in new
  869. code.
  870. */
  871. void LowerCase();
  872. /**
  873. Converts the first characters of the string to the upper case and all
  874. the subsequent ones to the lower case and returns the result.
  875. @since 2.9.0
  876. @see Capitalize()
  877. */
  878. wxString& MakeCapitalized();
  879. /**
  880. Converts all characters to lower case and returns the reference to the
  881. modified string.
  882. @see Lower()
  883. */
  884. wxString& MakeLower();
  885. /**
  886. Converts all characters to upper case and returns the reference to the
  887. modified string.
  888. @see Upper()
  889. */
  890. wxString& MakeUpper();
  891. /**
  892. Returns this string converted to upper case.
  893. @see MakeUpper()
  894. */
  895. wxString Upper() const;
  896. /**
  897. The same as MakeUpper().
  898. This is a wxWidgets 1.xx compatibility function; you should not use it in new
  899. code.
  900. */
  901. void UpperCase();
  902. //@}
  903. /**
  904. @member_group_name{search, Searching and replacing}
  905. These functions replace the standard @c strchr() and @c strstr()
  906. functions.
  907. See also the find(), rfind(), replace() STL-like functions.
  908. */
  909. //@{
  910. /**
  911. Searches for the given character @a ch.
  912. Returns the position or @c wxNOT_FOUND if not found.
  913. */
  914. int Find(wxUniChar ch, bool fromEnd = false) const;
  915. /**
  916. Searches for the given string @a sub.
  917. Returns the starting position or @c wxNOT_FOUND if not found.
  918. */
  919. int Find(const wxString& sub) const;
  920. /**
  921. Same as Find().
  922. This is a wxWidgets 1.xx compatibility function;
  923. you should not use it in new code.
  924. */
  925. int First(wxUniChar ch) const;
  926. /**
  927. Same as Find().
  928. This is a wxWidgets 1.xx compatibility function;
  929. you should not use it in new code.
  930. */
  931. int First(const wxString& str) const;
  932. /**
  933. Replace first (or all) occurrences of substring with another one.
  934. @param strOld
  935. The string to search for replacing.
  936. @param strNew
  937. The substitution string.
  938. @param replaceAll
  939. If @true a global replace will be done (default), otherwise only the
  940. first occurrence will be replaced.
  941. Returns the number of replacements made.
  942. */
  943. size_t Replace(const wxString& strOld, const wxString& strNew,
  944. bool replaceAll = true);
  945. //@}
  946. /**
  947. @member_group_name{numconv, Conversion to numbers}
  948. The string provides functions for conversion to signed and unsigned integer and
  949. floating point numbers.
  950. All functions take a pointer to the variable to put the numeric value
  951. in and return @true if the @b entire string could be converted to a
  952. number. Notice if there is a valid number in the beginning of the
  953. string, it is returned in the output parameter even if the function
  954. returns @false because there is more text following it.
  955. */
  956. //@{
  957. /**
  958. Attempts to convert the string to a floating point number.
  959. Returns @true on success (the number is stored in the location pointed to by
  960. @a val) or @false if the string does not represent such number (the value of
  961. @a val may still be modified in this case).
  962. Note that unlike ToCDouble() this function uses a localized version of
  963. @c wxStrtod() and thus needs as decimal point (and thousands separator) the
  964. locale-specific decimal point. Thus you should use this function only when
  965. you are sure that this string contains a floating point number formatted with
  966. the rules of the locale currently in use (see wxLocale).
  967. Also notice that even this function is locale-specific it does not
  968. support strings with thousands separators in them, even if the current
  969. locale uses digits grouping. You may use wxNumberFormatter::FromString()
  970. to parse such strings.
  971. Please refer to the documentation of the standard function @c strtod()
  972. for more details about the supported syntax.
  973. @see ToCDouble(), ToLong(), ToULong()
  974. */
  975. bool ToDouble(double* val) const;
  976. /**
  977. Variant of ToDouble() always working in "C" locale.
  978. Works like ToDouble() but unlike it this function expects the floating point
  979. number to be formatted always with the rules dictated by the "C" locale
  980. (in particular, the decimal point must be a dot), independently from the
  981. current application-wide locale (see wxLocale).
  982. @see ToDouble(), ToLong(), ToULong()
  983. */
  984. bool ToCDouble(double* val) const;
  985. /**
  986. Attempts to convert the string to a signed integer in base @a base.
  987. Returns @true on success in which case the number is stored in the location
  988. pointed to by @a val or @false if the string does not represent a
  989. valid number in the given base (the value of @a val may still be
  990. modified in this case).
  991. The value of @a base must be comprised between 2 and 36, inclusive, or
  992. be a special value 0 which means that the usual rules of @c C numbers are
  993. applied: if the number starts with @c 0x it is considered to be in base
  994. 16, if it starts with @c 0 - in base 8 and in base 10 otherwise. Note
  995. that you may not want to specify the base 0 if you are parsing the numbers
  996. which may have leading zeroes as they can yield unexpected (to the user not
  997. familiar with C) results.
  998. Note that unlike ToCLong() this function uses a localized version of
  999. @c wxStrtol(). Thus you should use this function only when you are sure
  1000. that this string contains an integer number formatted with
  1001. the rules of the locale currently in use (see wxLocale).
  1002. As with ToDouble(), this function does not support strings containing
  1003. thousands separators even if the current locale uses digits grouping.
  1004. You may use wxNumberFormatter::FromString() to parse such strings.
  1005. Please refer to the documentation of the standard function @c strtol()
  1006. for more details about the supported syntax.
  1007. @see ToCDouble(), ToDouble(), ToULong()
  1008. */
  1009. bool ToLong(long* val, int base = 10) const;
  1010. /**
  1011. Variant of ToLong() always working in "C" locale.
  1012. Works like ToLong() but unlike it this function expects the integer
  1013. number to be formatted always with the rules dictated by the "C" locale,
  1014. independently from the current application-wide locale (see wxLocale).
  1015. @see ToDouble(), ToLong(), ToULong()
  1016. */
  1017. bool ToCLong(long* val, int base = 10) const;
  1018. /**
  1019. This is exactly the same as ToLong() but works with 64 bit integer numbers.
  1020. Notice that currently it doesn't work (always returns @false) if parsing of 64
  1021. bit numbers is not supported by the underlying C run-time library. Compilers
  1022. with C99 support and Microsoft Visual C++ version 7 and higher do support this.
  1023. @see ToLong(), ToULongLong()
  1024. */
  1025. bool ToLongLong(wxLongLong_t* val, int base = 10) const;
  1026. /**
  1027. Attempts to convert the string to an unsigned integer in base @a base.
  1028. Returns @true on success in which case the number is stored in the
  1029. location pointed to by @a val or @false if the string does not
  1030. represent a valid number in the given base (the value of @a val may
  1031. still be modified in this case).
  1032. Please notice that this function behaves in the same way as the standard
  1033. @c strtoul() and so it simply converts negative numbers to unsigned
  1034. representation instead of rejecting them (e.g. -1 is returned as @c ULONG_MAX).
  1035. See ToLong() for the more detailed description of the @a base parameter
  1036. (and of the locale-specific behaviour of this function).
  1037. @see ToCULong(), ToDouble(), ToLong()
  1038. */
  1039. bool ToULong(unsigned long* val, int base = 10) const;
  1040. /**
  1041. Variant of ToULong() always working in "C" locale.
  1042. Works like ToULong() but unlike it this function expects the integer
  1043. number to be formatted always with the rules dictated by the "C" locale,
  1044. independently from the current application-wide locale (see wxLocale).
  1045. @see ToDouble(), ToLong(), ToULong()
  1046. */
  1047. bool ToCULong(unsigned long* val, int base = 10) const;
  1048. /**
  1049. This is exactly the same as ToULong() but works with 64 bit integer
  1050. numbers.
  1051. Please see ToLongLong() for additional remarks.
  1052. */
  1053. bool ToULongLong(wxULongLong_t* val, int base = 10) const;
  1054. //@}
  1055. /**
  1056. @member_group_name{fmt, Formatting and printing}
  1057. Both formatted versions (Printf/() and stream-like insertion operators
  1058. exist (for basic types only).
  1059. See also the static Format() and FormatV() functions.
  1060. */
  1061. //@{
  1062. /**
  1063. Similar to the standard function @e sprintf(). Returns the number of
  1064. characters written, or an integer less than zero on error.
  1065. Note that if @c wxUSE_PRINTF_POS_PARAMS is set to 1, then this function supports
  1066. Unix98-style positional parameters:
  1067. @code
  1068. wxString str;
  1069. str.Printf(wxT("%d %d %d"), 1, 2, 3);
  1070. // str now contains "1 2 3"
  1071. str.Printf(wxT("%2$d %3$d %1$d"), 1, 2, 3);
  1072. // str now contains "2 3 1"
  1073. @endcode
  1074. @note This function will use a safe version of @e vsprintf() (usually called
  1075. @e vsnprintf()) whenever available to always allocate the buffer of correct
  1076. size. Unfortunately, this function is not available on all platforms and the
  1077. dangerous @e vsprintf() will be used then which may lead to buffer overflows.
  1078. */
  1079. int Printf(const wxString& pszFormat, ...);
  1080. /**
  1081. Similar to vprintf. Returns the number of characters written, or an integer
  1082. less than zero
  1083. on error.
  1084. */
  1085. int PrintfV(const wxString& pszFormat, va_list argPtr);
  1086. //@}
  1087. /**
  1088. @member_group_name{mem, Memory management}
  1089. The following are "advanced" functions and they will be needed rarely.
  1090. Alloc() and Shrink() are only interesting for optimization purposes.
  1091. wxStringBuffer and wxStringBufferLength classes may be very useful when working
  1092. with some external API which requires the caller to provide a writable buffer.
  1093. See also the reserve() and resize() STL-like functions.
  1094. */
  1095. //@{
  1096. /**
  1097. Preallocate enough space for wxString to store @a nLen characters.
  1098. Please note that this method does the same thing as the standard
  1099. reserve() one and shouldn't be used in new code.
  1100. This function may be used to increase speed when the string is
  1101. constructed by repeated concatenation as in
  1102. @code
  1103. // delete all vowels from the string
  1104. wxString DeleteAllVowels(const wxString& original)
  1105. {
  1106. wxString result;
  1107. size_t len = original.length();
  1108. result.Alloc(len);
  1109. for ( size_t n = 0; n < len; n++ )
  1110. {
  1111. if ( strchr("aeuio", tolower(original[n])) == NULL )
  1112. result += original[n];
  1113. }
  1114. return result;
  1115. }
  1116. @endcode
  1117. because it will avoid the need to reallocate string memory many times
  1118. (in case of long strings). Note that it does not set the maximal length
  1119. of a string -- it will still expand if more than @a nLen characters are
  1120. stored in it. Also, it does not truncate the existing string (use
  1121. Truncate() for this) even if its current length is greater than @a nLen.
  1122. @return @true if memory was successfully allocated, @false otherwise.
  1123. */
  1124. bool Alloc(size_t nLen);
  1125. /**
  1126. Minimizes the string's memory. This can be useful after a call to
  1127. Alloc() if too much memory were preallocated.
  1128. */
  1129. bool Shrink();
  1130. /**
  1131. Returns a deep copy of the string.
  1132. That is, the returned string is guaranteed to not share data with this
  1133. string when using reference-counted wxString implementation.
  1134. This method is primarily useful for passing strings between threads
  1135. (because wxString is not thread-safe). Unlike creating a copy using
  1136. @c wxString(c_str()), Clone() handles embedded NULs correctly.
  1137. @since 2.9.0
  1138. */
  1139. wxString Clone() const;
  1140. /**
  1141. Empties the string and frees memory occupied by it.
  1142. @see Empty()
  1143. */
  1144. void Clear();
  1145. //@}
  1146. /**
  1147. @member_group_name{misc, Miscellaneous}
  1148. Miscellaneous other string functions.
  1149. */
  1150. //@{
  1151. /**
  1152. Returns @true if target appears anywhere in wxString; else @false.
  1153. This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
  1154. */
  1155. bool Contains(const wxString& str) const;
  1156. /**
  1157. Makes the string empty, but doesn't free memory occupied by the string.
  1158. @see Clear().
  1159. */
  1160. void Empty();
  1161. /**
  1162. Returns the number of occurrences of @e ch in the string.
  1163. This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
  1164. */
  1165. int Freq(wxUniChar ch) const;
  1166. /**
  1167. Returns @true if the string contains only ASCII characters.
  1168. See wxUniChar::IsAscii for more details.
  1169. This is a wxWidgets 1.xx compatibility function; you should not use it in new
  1170. code.
  1171. */
  1172. bool IsAscii() const;
  1173. /**
  1174. Returns @true if the string is an integer (with possible sign).
  1175. This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
  1176. */
  1177. bool IsNumber() const;
  1178. /**
  1179. Returns @true if the string is a word.
  1180. This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
  1181. */
  1182. bool IsWord() const;
  1183. /**
  1184. Adds @a count copies of @a chPad to the beginning, or to the end of the
  1185. string (the default).
  1186. Removes spaces from the left or from the right (default).
  1187. */
  1188. wxString& Pad(size_t count, wxUniChar chPad = ' ', bool fromRight = true);
  1189. /**
  1190. Removes all characters from the string starting at @a pos.
  1191. Use Truncate() as a more readable alternative.
  1192. This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
  1193. */
  1194. wxString& Remove(size_t pos);
  1195. /**
  1196. Removes @a len characters from the string, starting at @a pos.
  1197. This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
  1198. */
  1199. wxString& Remove(size_t pos, size_t len);
  1200. /**
  1201. Removes the last character.
  1202. */
  1203. wxString& RemoveLast(size_t n = 1);
  1204. /**
  1205. Strip characters at the front and/or end.
  1206. This is the same as Trim() except that it doesn't change this string.
  1207. This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
  1208. */
  1209. wxString Strip(stripType s = trailing) const;
  1210. /**
  1211. Removes white-space (space, tabs, form feed, newline and carriage return) from
  1212. the left or from the right end of the string (right is default).
  1213. */
  1214. wxString& Trim(bool fromRight = true);
  1215. /**
  1216. Truncate the string to the given length.
  1217. */
  1218. wxString& Truncate(size_t len);
  1219. //@}
  1220. /**
  1221. @member_group_name{iter, Iterator interface}
  1222. These methods return iterators to the beginning or end of the string.
  1223. Please see any STL reference (e.g. http://www.cppreference.com/wiki/string/start)
  1224. for their documentation.
  1225. */
  1226. //@{
  1227. const_iterator begin() const;
  1228. iterator begin();
  1229. const_iterator end() const;
  1230. iterator end();
  1231. const_reverse_iterator rbegin() const;
  1232. reverse_iterator rbegin();
  1233. const_reverse_iterator rend() const;
  1234. reverse_iterator rend();
  1235. //@}
  1236. /**
  1237. @member_group_name{stl, STL interface}
  1238. The supported STL functions are listed here.
  1239. Please see any STL reference (e.g. http://www.cppreference.com/wiki/string/start)
  1240. for their documentation.
  1241. */
  1242. //@{
  1243. wxString& append(const wxString& str, size_t pos, size_t n);
  1244. wxString& append(const wxString& str);
  1245. wxString& append(const char *sz, size_t n);
  1246. wxString& append(const wchar_t *sz, size_t n);
  1247. wxString& append(size_t n, wxUniChar ch);
  1248. wxString& append(const_iterator first, const_iterator last);
  1249. wxString& assign(const wxString& str, size_t pos, size_t n);
  1250. wxString& assign(const wxString& str);
  1251. wxString& assign(const char *sz, size_t n);
  1252. wxString& assign(const wchar_t *sz, size_t n);
  1253. wxString& assign(size_t n, wxUniChar ch);
  1254. wxString& assign(const_iterator first, const_iterator last);
  1255. wxUniChar at(size_t n) const;
  1256. wxUniCharRef at(size_t n);
  1257. void clear();
  1258. size_type capacity() const;
  1259. int compare(const wxString& str) const;
  1260. int compare(size_t nStart, size_t nLen, const wxString& str) const;
  1261. int compare(size_t nStart, size_t nLen,
  1262. const wxString& str, size_t nStart2, size_t nLen2) const;
  1263. int compare(size_t nStart, size_t nLen,
  1264. const char* sz, size_t nCount = npos) const;
  1265. int compare(size_t nStart, size_t nLen,
  1266. const wchar_t* sz, size_t nCount = npos) const;
  1267. wxCStrData data() const;
  1268. bool empty() const;
  1269. wxString& erase(size_type pos = 0, size_type n = npos);
  1270. iterator erase(iterator first, iterator last);
  1271. iterator erase(iterator first);
  1272. size_t find(const wxString& str, size_t nStart = 0) const;
  1273. size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const;
  1274. size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const;
  1275. size_t find(wxUniChar ch, size_t nStart = 0) const;
  1276. size_t find_first_of(const char* sz, size_t nStart = 0) const;
  1277. size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
  1278. size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
  1279. size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
  1280. size_t find_first_of(wxUniChar c, size_t nStart = 0) const;
  1281. size_t find_last_of (const wxString& str, size_t nStart = npos) const;
  1282. size_t find_last_of (const char* sz, size_t nStart = npos) const;
  1283. size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
  1284. size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
  1285. size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
  1286. size_t find_last_of(wxUniChar c, size_t nStart = npos) const;
  1287. size_t find_first_not_of(const wxString& str, size_t nStart = 0) const;
  1288. size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
  1289. size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
  1290. size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
  1291. size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
  1292. size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
  1293. size_t find_last_not_of(const wxString& str, size_t nStart = npos) const;
  1294. size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
  1295. size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
  1296. size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
  1297. size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
  1298. wxString& insert(size_t nPos, const wxString& str);
  1299. wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n);
  1300. wxString& insert(size_t nPos, const char *sz, size_t n);
  1301. wxString& insert(size_t nPos, const wchar_t *sz, size_t n);
  1302. wxString& insert(size_t nPos, size_t n, wxUniChar ch);
  1303. iterator insert(iterator it, wxUniChar ch);
  1304. void insert(iterator it, const_iterator first, const_iterator last);
  1305. void insert(iterator it, size_type n, wxUniChar ch);
  1306. size_t length() const;
  1307. size_type max_size() const;
  1308. void reserve(size_t sz);
  1309. void resize(size_t nSize, wxUniChar ch = '\0');
  1310. wxString& replace(size_t nStart, size_t nLen, const wxString& str);
  1311. wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch);
  1312. wxString& replace(size_t nStart, size_t nLen,
  1313. const wxString& str, size_t nStart2, size_t nLen2);
  1314. wxString& replace(size_t nStart, size_t nLen,
  1315. const char* sz, size_t nCount);
  1316. wxString& replace(size_t nStart, size_t nLen,
  1317. const wchar_t* sz, size_t nCount);
  1318. wxString& replace(size_t nStart, size_t nLen,
  1319. const wxString& s, size_t nCount);
  1320. wxString& replace(iterator first, iterator last, const wxString& s);
  1321. wxString& replace(iterator first, iterator last, const char* s, size_type n);
  1322. wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n);
  1323. wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch);
  1324. wxString& replace(iterator first, iterator last,
  1325. const_iterator first1, const_iterator last1);
  1326. wxString& replace(iterator first, iterator last,
  1327. const char *first1, const char *last1);
  1328. wxString& replace(iterator first, iterator last,
  1329. const wchar_t *first1, const wchar_t *last1);
  1330. size_t rfind(const wxString& str, size_t nStart = npos) const;
  1331. size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const;
  1332. size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const;
  1333. size_t rfind(wxUniChar ch, size_t nStart = npos) const;
  1334. size_type size() const;
  1335. wxString substr(size_t nStart = 0, size_t nLen = npos) const;
  1336. void swap(wxString& str);
  1337. //@}
  1338. // STATIC FUNCTIONS
  1339. // Keep these functions separated from the other groups or Doxygen gets confused
  1340. // -----------------------------------------------------------------------------
  1341. /**
  1342. An 'invalid' value for string index
  1343. */
  1344. static const size_t npos;
  1345. /**
  1346. This static function returns the string containing the result of calling
  1347. Printf() with the passed parameters on it.
  1348. @see FormatV(), Printf()
  1349. */
  1350. static wxString Format(const wxString& format, ...);
  1351. /**
  1352. This static function returns the string containing the result of calling
  1353. PrintfV() with the passed parameters on it.
  1354. @see Format(), PrintfV()
  1355. */
  1356. static wxString FormatV(const wxString& format, va_list argptr);
  1357. //@{
  1358. /**
  1359. Converts given buffer of binary data from 8-bit string to wxString. In
  1360. Unicode build, the string is interpreted as being in ISO-8859-1
  1361. encoding. The version without @e len parameter takes NUL-terminated
  1362. data.
  1363. This is a convenience method useful when storing binary data in
  1364. wxString. It should be used @em only for that purpose and only in
  1365. conjunction with To8BitData(). Use mb_str() for conversion of character
  1366. data to known encoding.
  1367. @since 2.8.4
  1368. @see wxString::To8BitData()
  1369. */
  1370. static wxString From8BitData(const char* buf, size_t len);
  1371. static wxString From8BitData(const char* buf);
  1372. //@}
  1373. //@{
  1374. /**
  1375. Converts the string or character from an ASCII, 7-bit form
  1376. to the native wxString representation.
  1377. */
  1378. static wxString FromAscii(const char* s);
  1379. static wxString FromAscii(const unsigned char* s);
  1380. static wxString FromAscii(const char* s, size_t len);
  1381. static wxString FromAscii(const unsigned char* s, size_t len);
  1382. static wxString FromAscii(char c);
  1383. //@}
  1384. /**
  1385. Returns a string with the textual representation of the number in C
  1386. locale.
  1387. Unlike FromDouble() the string returned by this function always uses
  1388. the period character as decimal separator, independently of the current
  1389. locale. Otherwise its behaviour is identical to the other function.
  1390. @since 2.9.1
  1391. @see ToCDouble()
  1392. */
  1393. static wxString FromCDouble(double val, int precision = -1);
  1394. /**
  1395. Returns a string with the textual representation of the number.
  1396. For the default value of @a precision, this function behaves as a
  1397. simple wrapper for @code wxString::Format("%g", val) @endcode. If @a
  1398. precision is positive (or zero), the @c %.Nf format is used with the
  1399. given precision value.
  1400. Notice that the string returned by this function uses the decimal
  1401. separator appropriate for the current locale, e.g. @c "," and not a
  1402. period in French locale. Use FromCDouble() if this is unwanted.
  1403. @param val
  1404. The value to format.
  1405. @param precision
  1406. The number of fractional digits to use in or -1 to use the most
  1407. appropriate format. This parameter is new in wxWidgets 2.9.2.
  1408. @since 2.9.1
  1409. @see ToDouble()
  1410. */
  1411. static wxString FromDouble(double val, int precision = -1);
  1412. //@{
  1413. /**
  1414. Converts C string encoded in UTF-8 to wxString.
  1415. If @a s is not a valid UTF-8 string, an empty string is returned.
  1416. Notice that when using UTF-8 wxWidgets build there is a more efficient
  1417. alternative to this function called FromUTF8Unchecked() which, unlike
  1418. this one, doesn't check that the input string is valid.
  1419. @since 2.8.4
  1420. */
  1421. static wxString FromUTF8(const char* s);
  1422. static wxString FromUTF8(const char* s, size_t len);
  1423. //@}
  1424. //@{
  1425. /**
  1426. Converts C string encoded in UTF-8 to wxString without checking its
  1427. validity.
  1428. This method assumes that @a s is a valid UTF-8 sequence and doesn't do
  1429. any validation (although an assert failure is triggered in debug builds
  1430. if the string is invalid). Only use it if you are absolutely sure that
  1431. @a s is a correct UTF-8 string (e.g. because it comes from another
  1432. library using UTF-8) and if the performance matters, otherwise use
  1433. slower (in UTF-8 build) but safer FromUTF8(). Passing a bad UTF-8
  1434. string to this function will result in creating a corrupted wxString
  1435. and all the subsequent operations on it will be undefined.
  1436. @since 2.8.9
  1437. */
  1438. static wxString FromUTF8Unchecked(const char* s);
  1439. static wxString FromUTF8Unchecked(const char* s, size_t len);
  1440. //@}
  1441. };
  1442. //@{
  1443. /**
  1444. Comparison operator for string types.
  1445. */
  1446. inline bool operator==(const wxString& s1, const wxString& s2);
  1447. inline bool operator!=(const wxString& s1, const wxString& s2);
  1448. inline bool operator< (const wxString& s1, const wxString& s2);
  1449. inline bool operator> (const wxString& s1, const wxString& s2);
  1450. inline bool operator<=(const wxString& s1, const wxString& s2);
  1451. inline bool operator>=(const wxString& s1, const wxString& s2);
  1452. inline bool operator==(const wxString& s1, const wxCStrData& s2);
  1453. inline bool operator==(const wxCStrData& s1, const wxString& s2);
  1454. inline bool operator!=(const wxString& s1, const wxCStrData& s2);
  1455. inline bool operator!=(const wxCStrData& s1, const wxString& s2);
  1456. inline bool operator==(const wxString& s1, const wxWCharBuffer& s2);
  1457. inline bool operator==(const wxWCharBuffer& s1, const wxString& s2);
  1458. inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2);
  1459. inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2);
  1460. inline bool operator==(const wxString& s1, const wxCharBuffer& s2);
  1461. inline bool operator==(const wxCharBuffer& s1, const wxString& s2);
  1462. inline bool operator!=(const wxString& s1, const wxCharBuffer& s2);
  1463. inline bool operator!=(const wxCharBuffer& s1, const wxString& s2);
  1464. //@}
  1465. //@{
  1466. /**
  1467. Comparison operators char types.
  1468. */
  1469. inline bool operator==(const wxUniChar& c, const wxString& s);
  1470. inline bool operator==(const wxUniCharRef& c, const wxString& s);
  1471. inline bool operator==(char c, const wxString& s);
  1472. inline bool operator==(wchar_t c, const wxString& s);
  1473. inline bool operator==(int c, const wxString& s);
  1474. inline bool operator==(const wxString& s, const wxUniChar& c);
  1475. inline bool operator==(const wxString& s, const wxUniCharRef& c);
  1476. inline bool operator==(const wxString& s, char c);
  1477. inline bool operator==(const wxString& s, wchar_t c);
  1478. inline bool operator!=(const wxUniChar& c, const wxString& s);
  1479. inline bool operator!=(const wxUniCharRef& c, const wxString& s);
  1480. inline bool operator!=(char c, const wxString& s);
  1481. inline bool operator!=(wchar_t c, const wxString& s);
  1482. inline bool operator!=(int c, const wxString& s);
  1483. inline bool operator!=(const wxString& s, const wxUniChar& c);
  1484. inline bool operator!=(const wxString& s, const wxUniCharRef& c);
  1485. inline bool operator!=(const wxString& s, char c);
  1486. inline bool operator!=(const wxString& s, wchar_t c);
  1487. //@}
  1488. /**
  1489. The global wxString instance of an empty string.
  1490. Used extensively in the entire wxWidgets API.
  1491. */
  1492. wxString wxEmptyString;
  1493. /**
  1494. @class wxStringBufferLength
  1495. This tiny class allows you to conveniently access the wxString internal buffer
  1496. as a writable pointer without any risk of forgetting to restore the string to
  1497. the usable state later, and allows the user to set the internal length of the string.
  1498. For example, assuming you have a low-level OS function called
  1499. @c "int GetMeaningOfLifeAsString(char *)" copying the value in the provided
  1500. buffer (which must be writable, of course), and returning the actual length
  1501. of the string, you might call it like this:
  1502. @code
  1503. wxString theAnswer;
  1504. wxStringBufferLength theAnswerBuffer(theAnswer, 1024);
  1505. int nLength = GetMeaningOfLifeAsString(theAnswerBuffer);
  1506. theAnswerBuffer.SetLength(nLength);
  1507. if ( theAnswer != "42" )
  1508. wxLogError("Something is very wrong!");
  1509. @endcode
  1510. Note that the exact usage of this depends on whether or not wxUSE_STL is
  1511. enabled. If wxUSE_STL is enabled, wxStringBuffer creates a separate empty
  1512. character buffer, and if wxUSE_STL is disabled, it uses GetWriteBuf() from
  1513. wxString, keeping the same buffer wxString uses intact. In other words,
  1514. relying on wxStringBuffer containing the old wxString data is not a good
  1515. idea if you want to build your program both with and without wxUSE_STL.
  1516. Note that wxStringBuffer::SetLength @b must be called before
  1517. wxStringBufferLength destructs.
  1518. @library{wxbase}
  1519. @category{data}
  1520. */
  1521. class wxStringBufferLength
  1522. {
  1523. public:
  1524. /**
  1525. Constructs a writable string buffer object associated with the given string
  1526. and containing enough space for at least @a len characters.
  1527. Basically, this is equivalent to calling wxString::GetWriteBuf and
  1528. saving the result.
  1529. */
  1530. wxStringBufferLength(const wxString& str, size_t len);
  1531. /**
  1532. Restores the string passed to the constructor to the usable state by calling
  1533. wxString::UngetWriteBuf on it.
  1534. */
  1535. ~wxStringBufferLength();
  1536. /**
  1537. Sets the internal length of the string referred to by wxStringBufferLength to
  1538. @a nLength characters.
  1539. Must be called before wxStringBufferLength destructs.
  1540. */
  1541. void SetLength(size_t nLength);
  1542. /**
  1543. Returns the writable pointer to a buffer of the size at least equal to the
  1544. length specified in the constructor.
  1545. */
  1546. wxChar* operator wxChar *();
  1547. };
  1548. /**
  1549. @class wxStringBuffer
  1550. This tiny class allows you to conveniently access the wxString internal buffer
  1551. as a writable pointer without any risk of forgetting to restore the string
  1552. to the usable state later.
  1553. For example, assuming you have a low-level OS function called
  1554. @c "GetMeaningOfLifeAsString(char *)" returning the value in the provided
  1555. buffer (which must be writable, of course) you might call it like this:
  1556. @code
  1557. wxString theAnswer;
  1558. GetMeaningOfLifeAsString(wxStringBuffer(theAnswer, 1024));
  1559. if ( theAnswer != "42" )
  1560. wxLogError("Something is very wrong!");
  1561. @endcode
  1562. Note that the exact usage of this depends on whether or not @c wxUSE_STL is
  1563. enabled. If @c wxUSE_STL is enabled, wxStringBuffer creates a separate empty
  1564. character buffer, and if @c wxUSE_STL is disabled, it uses GetWriteBuf() from
  1565. wxString, keeping the same buffer wxString uses intact. In other words,
  1566. relying on wxStringBuffer containing the old wxString data is not a good
  1567. idea if you want to build your program both with and without @c wxUSE_STL.
  1568. @library{wxbase}
  1569. @category{data}
  1570. */
  1571. class wxStringBuffer
  1572. {
  1573. public:
  1574. /**
  1575. Constructs a writable string buffer object associated with the given string
  1576. and containing enough space for at least @a len characters.
  1577. Basically, this is equivalent to calling wxString::GetWriteBuf() and
  1578. saving the result.
  1579. */
  1580. wxStringBuffer(const wxString& str, size_t len);
  1581. /**
  1582. Restores the string passed to the constructor to the usable state by calling
  1583. wxString::UngetWriteBuf() on it.
  1584. */
  1585. ~wxStringBuffer();
  1586. /**
  1587. Returns the writable pointer to a buffer of the size at least equal to the
  1588. length specified in the constructor.
  1589. */
  1590. wxStringCharType* operator wxStringCharType *();
  1591. };
  1592. /** @addtogroup group_funcmacro_string */
  1593. //@{
  1594. /**
  1595. Allows to extend a function with the signature:
  1596. @code bool SomeFunc(const wxUniChar& c) @endcode
  1597. which operates on a single character, to an entire wxString.
  1598. E.g. if you want to check if an entire string contains only digits,
  1599. you can do:
  1600. @code
  1601. if (wxStringCheck<wxIsdigit>(myString))
  1602. ... // the entire string contains only digits!
  1603. else
  1604. ... // at least one character of myString is not a digit
  1605. @endcode
  1606. @return @true if the given function returns a non-zero value for all
  1607. characters of the @a val string.
  1608. */
  1609. template<bool (T)(const wxUniChar& c)>
  1610. inline bool wxStringCheck(const wxString& val);
  1611. //@}