BeStringUtilities.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 #ifndef BENTLEY_WIN32
10 #define BENTLEY_WIN32
11 #endif
12 
14 
15 #include "Bentley.h"
16 #include "bvector.h"
17 #include "NonCopyableClass.h"
18 #include "CodePages.h"
19 #include <stdarg.h>
20 #include <stdio.h>
21 
23 
26 
27 typedef bvector<Utf16Char> Utf16Buffer;
28 typedef Utf16Buffer const* Utf16BufferCP;
29 typedef Utf16Buffer const& Utf16BufferCR;
32 
33 // Options for formatting unsigned integer values in hexadecimal
34 enum class HexFormatOptions
35  {
36  None = 0,
37  LeftJustify = 1 << 0, // Append spaces to meet minimum width. Analogous to '-' printf option.
38  IncludePrefix = 1 << 1, // Prefix with "0x" or "0X" depending on the HexFormatOptions::Uppercase flag. Analagous to '#' printf option.
39  Uppercase = 1 << 2, // Use uppercase hexadecimal digits; also affects case of prefix. Analogous to "%X" printf specifier.
40  LeadingZeros = 1 << 3, // Pad with leading zeros to meet minimum precision and/or width. Analogous to '0' printf option.
41  UsePrecision = 1 << 4, // Without this option, default precision of 1 is used and leading zeros are applied to fulfill minimum width based on HexFormatOptions::LeadingZeros option.
42  // With this option, leading zeros are not used to satisfy minimum width regardless of HexFormatOptions::LeadingZeros, but are used to satisfy minimum precision, in keeping with Snwprintf()
43  };
44 
45 //=======================================================================================
53 // @bsiclass Bentley Systems
54 //=======================================================================================
56  {
57 private:
58  // Static class; don't allow new instances.
59  BeStringUtilities () { }
60 
61 
62 public:
63  static const size_t NPOS = (size_t)-1;
64  static const size_t AsManyAsPossible = ((size_t)-1);
65 
73  BENTLEYDLL_EXPORT static BentleyStatus Utf16ToWChar (WStringR, Utf16CP, size_t count = AsManyAsPossible);
74 
84  BENTLEYDLL_EXPORT static BentleyStatus Utf16ToWChar (WCharP wbuf, size_t wbufSizeInChars, Utf16CP inStr, size_t count = AsManyAsPossible);
85 
94  template <size_t wbufSizeInChars>
95  static BentleyStatus Utf16ToWChar (WChar (&wbuf)[wbufSizeInChars], Utf16CP inStr, size_t count = AsManyAsPossible) {return Utf16ToWChar (wbuf, wbufSizeInChars, inStr, count);}
96 
105  BENTLEYDLL_EXPORT static BentleyStatus WCharToUtf16 (Utf16BufferR, WCharCP, size_t count = AsManyAsPossible);
106 
116  BENTLEYDLL_EXPORT static BentleyStatus WCharToUtf16 (Utf16P ubuf, size_t ubufSizeInChars, WCharCP inStr, size_t count = AsManyAsPossible);
117 
124  template <size_t ubufSizeInChars>
125  static BentleyStatus WCharToUtf16 (Utf16Char (&ubuf)[ubufSizeInChars], WCharCP inStr, size_t count) {return WCharToUtf16 (ubuf, ubufSizeInChars, inStr, count);}
126 
128  BENTLEYDLL_EXPORT static int CompareUtf16 (Utf16CP s1, Utf16CP s2);
129 
131  BENTLEYDLL_EXPORT static int CompareUtf16WChar (Utf16CP s1, WCharCP s2);
132 
134  BENTLEYDLL_EXPORT static size_t Utf16Len (Utf16CP);
135 
137  BENTLEYDLL_EXPORT static void CopyUtf16 (Utf16P outStr, size_t outStrCapacity, Utf16CP inStr);
138 
140  template <size_t outStrCapacity>
141  static void CopyUtf16 (Utf16Char (&outStr)[outStrCapacity], Utf16CP inStr) {return CopyUtf16 (outStr, outStrCapacity, inStr);}
142 
148  BENTLEYDLL_EXPORT static BentleyStatus WCharToUtf8 (Utf8StringR, WCharCP, size_t count = AsManyAsPossible);
149 
155  BENTLEYDLL_EXPORT static BentleyStatus Utf8ToWChar (WStringR, Utf8CP, size_t count = AsManyAsPossible);
156 
164  BENTLEYDLL_EXPORT static WCharP Utf8ToWChar (WCharP outWChar, Utf8CP inChar, size_t outMaxChars);
165 
171  BENTLEYDLL_EXPORT static BentleyStatus Utf8ToUtf16 (Utf16BufferR, Utf8CP, size_t count = AsManyAsPossible);
172 
178  BENTLEYDLL_EXPORT static BentleyStatus Utf16ToUtf8 (Utf8StringR, Utf16CP, size_t count = AsManyAsPossible);
179 
184  BENTLEYDLL_EXPORT static BentleyStatus WCharToLocaleChar (AStringR, UInt32 codePage, WCharCP, size_t count = AsManyAsPossible);
185 
192  BENTLEYDLL_EXPORT static CharP WCharToCurrentLocaleChar (CharP outChar, WCharCP inWChar, size_t outMaxBytes);
193 
197  BENTLEYDLL_EXPORT static BentleyStatus WCharToCurrentLocaleChar (AStringR localeStr, WCharCP inWChar);
198 
201  BENTLEYDLL_EXPORT static void* WCharToPointer (WCharCP inWChar);
202 
207  BENTLEYDLL_EXPORT static BentleyStatus LocaleCharToWChar (WStringR, CharCP, UInt32 codePage, size_t count = AsManyAsPossible);
208 
215  BENTLEYDLL_EXPORT static WCharP LocaleCharToWChar (WCharP outWChar, CharCP inChar, UInt32 codePage, size_t outMaxChars);
216 
223  BENTLEYDLL_EXPORT static WCharP CurrentLocaleCharToWChar (WCharP outWChar, CharCP inChar, size_t outMaxChars);
224 
228  BENTLEYDLL_EXPORT static BentleyStatus CurrentLocaleCharToWChar (WStringR outStr, CharCP inChar);
229 
233  BENTLEYDLL_EXPORT static BentleyStatus GetCurrentCodePage (LangCodePage& codePage);
234 
238  BENTLEYDLL_EXPORT static bool IsValidCodePage (LangCodePage codePage);
239 
242  BENTLEYDLL_EXPORT static BentleyStatus GetDecimalSeparator (WStringR sep);
243 
247  BENTLEYDLL_EXPORT static Utf8String UriEncode (Utf8CP charsToEncode);
248 
255  BENTLEYDLL_EXPORT static int Snprintf (CharP buffer, size_t numCharsInBuffer, CharCP format, ...);
256 
262  template <size_t destArraySize>
263  static int Snprintf (char (&destArray)[destArraySize], CharCP format, ...)
264  {
265  va_list args;
266  va_start (args, format);
267  return Vsnprintf (destArray, destArraySize, format, args);
268  }
269 
277  BENTLEYDLL_EXPORT static int Vsnprintf (CharP buffer, size_t numCharsInBuffer, CharCP format, va_list args);
278 
285  BENTLEYDLL_EXPORT static int Snwprintf (WCharP buffer, size_t numCharsInBuffer, WCharCP format, ...);
286 
292  template <size_t destArraySize>
293  static int Snwprintf (wchar_t (&destArray)[destArraySize], WCharCP format, ...)
294  {
295  va_list args;
296  va_start (args, format);
297  return Vsnwprintf (destArray, destArraySize, format, args);
298  }
299 
307  BENTLEYDLL_EXPORT static int Vsnwprintf (WCharP buffer, size_t numCharsInBuffer, WCharCP format, va_list args);
308 
310  BENTLEYDLL_EXPORT static char* Strlwr (char* s);
311 
313  BENTLEYDLL_EXPORT static wchar_t* Wcslwr (wchar_t* s);
314 
316  BENTLEYDLL_EXPORT static char* Strupr (char* s);
317 
319  BENTLEYDLL_EXPORT static wchar_t* Wcsupr (wchar_t* s);
320 
322  BENTLEYDLL_EXPORT static int Stricmp (const char* s1, const char* s2);
323 
325  BENTLEYDLL_EXPORT static int Wcsicmp (WCharCP lhs, WCharCP rhs);
326 
328  BENTLEYDLL_EXPORT static int Strnicmp (const char* s1, const char* s2, size_t n);
329 
331  BENTLEYDLL_EXPORT static int Wcsnicmp (const wchar_t* s1, const wchar_t* s2, size_t n);
332 
334  BENTLEYDLL_EXPORT static char* Strdup (char const* s);
335 
337  BENTLEYDLL_EXPORT static wchar_t* Wcsdup (wchar_t const* s);
338 
340  BENTLEYDLL_EXPORT static char *Strtok (char *strToken, const char *strDelimit, char **context);
341 
343  BENTLEYDLL_EXPORT static wchar_t *Wcstok (wchar_t *wcsToken, const wchar_t *wcsDelimit, wchar_t **context);
344 
346  BENTLEYDLL_EXPORT static wchar_t *Wcsrev (wchar_t *wstr);
347 
349  BENTLEYDLL_EXPORT static wchar_t* Wcsncpy (wchar_t *strDest, size_t destLen, const wchar_t *strSource, size_t count = AsManyAsPossible);
350 
352  template <size_t destArraySize>
353  static wchar_t* Wcsncpy (wchar_t (&destArray)[destArraySize], const wchar_t *strSource, size_t count = AsManyAsPossible) {return Wcsncpy (destArray, destArraySize, strSource, count);}
354 
356  BENTLEYDLL_EXPORT static char* Strncpy (char *strDest, size_t destLen, const char *strSource, size_t count = AsManyAsPossible);
357 
359  template <size_t destArraySize>
360  static char* Strncpy (char (&destArray)[destArraySize], const char *strSource, size_t count = AsManyAsPossible) {return Strncpy (destArray, destArraySize, strSource, count);}
361 
367  BENTLEYDLL_EXPORT static BentleyStatus ParseHexUInt64 (UInt64& value, WCharCP string);
368 
372  BENTLEYDLL_EXPORT static void FormatHexUInt64 (WCharP buf, UInt64 value);
373 
375  BENTLEYDLL_EXPORT static int Wtoi (wchar_t const* s);
376 
378  BENTLEYDLL_EXPORT static unsigned long Wcstoul (wchar_t const* nptr, wchar_t** endptr, int base);
379 
381  BENTLEYDLL_EXPORT static long Wcstol (wchar_t const* nptr, wchar_t** endptr, int base);
382 
384  BENTLEYDLL_EXPORT static double Wcstod (wchar_t const* s, wchar_t** end);
385 
386  BENTLEYDLL_EXPORT static double Wtof (wchar_t const* s);
387 
390  BENTLEYDLL_EXPORT static BentleyStatus Itow (wchar_t* buffer, int value, size_t sizeInCharacters, int radix);
391 
393  BENTLEYDLL_EXPORT static BentleyStatus Wmemmove (wchar_t *dest, size_t numberOfElements, const wchar_t *src, size_t count);
394 
396  BENTLEYDLL_EXPORT static BentleyStatus Memcpy (void *dest, size_t numberOfElements, const void *src, size_t count);
397 
399  BENTLEYDLL_EXPORT static BentleyStatus Wmemcpy (wchar_t *dest, size_t numberOfElements, const wchar_t *src, size_t count);
400 
405  BENTLEYDLL_EXPORT static void Split(WCharCP str, WCharCP delimiters, bvector<WString>& tokens) { return Split(str, delimiters, NULL, tokens); }
406 
412  BENTLEYDLL_EXPORT static void Split(WCharCP str, WCharCP delimiters, WCharCP escapeChars, bvector<WString>& tokens);
413 
418  BENTLEYDLL_EXPORT static WString Join (bvector<WString> const& strings, WCharCP delimiter = NULL);
419 
424  BENTLEYDLL_EXPORT static void Split(Utf8CP str, Utf8CP delimiters, bvector<Utf8String>& tokens) { return Split(str, delimiters, NULL, tokens); }
425 
431  BENTLEYDLL_EXPORT static void Split(Utf8CP str, Utf8CP delimiters, Utf8CP escapeChars, bvector<Utf8String>& tokens);
432 
437  BENTLEYDLL_EXPORT static void ParseArguments (bvector<WString>& subStrings, WCharCP inString, WCharCP auxDelimiters = NULL);
438 
445  BENTLEYDLL_EXPORT static UInt32 ParseArguments (WCharCP inString, UInt32 numSubStrings, ...);
446 
458  BENTLEYDLL_EXPORT static int FormatUInt64 (wchar_t *dest, size_t numCharsInBuffer, UInt64 val, HexFormatOptions opts, UInt8 width = 0, UInt8 precision = 1);
459 
465  BENTLEYDLL_EXPORT static BentleyStatus ParseUInt64(uint64_t& value, WCharCP string);
466 
472  BENTLEYDLL_EXPORT static BentleyStatus ParseUInt64(uint64_t& value, Utf8CP string);
473 
480  BENTLEYDLL_EXPORT static int LexicographicCompare (WCharCP value0, WCharCP value1);
481 
482  }; // BeStringUtilities
483 
486 
489 
492 
495 
497 
Contains a UTF-8 encoded string.
Definition: WString.h:275
char * CharP
Definition: Bentley.h:227
#define BENTLEYDLL_EXPORT
Definition: Bentley.h:249
Utf16Buffer * Utf16BufferP
Definition: BeStringUtilities.h:30
uint16_t Utf16Char
Definition: Bentley.r.h:155
static int Snprintf(char(&destArray)[destArraySize], CharCP format,...)
Format a string following the rules of sprintf.
Definition: BeStringUtilities.h:263
wchar_t * WCharP
Definition: Bentley.h:225
static BentleyStatus Utf16ToWChar(WChar(&wbuf)[wbufSizeInChars], Utf16CP inStr, size_t count=AsManyAsPossible)
Converts a UTF-16 buffer to a WChar array.
Definition: BeStringUtilities.h:95
A string class that has many of the same capabilities as std::string, plus additional functions such ...
Definition: WString.h:30
wchar_t WChar
Definition: Bentley.h:223
BentleyStatus BeGetUserNameA(AStringR s)
Get the current user name.
bvector< Utf16Char > Utf16Buffer
Definition: BeStringUtilities.h:27
Utf16Char const * Utf16CP
Definition: Bentley.h:231
BeStringUtilities is a number of useful static methods for working with strings.
Definition: BeStringUtilities.h:55
Utf16Buffer const * Utf16BufferCP
Definition: BeStringUtilities.h:28
BentleyStatus BeGetComputerNameA(AStringR s)
Get the current computer name.
BentleyStatus BeGetUserName(WStringR s)
Get the current user name.
iterator end()
Definition: stdcxx/bstdmap.h:186
HexFormatOptions
Definition: BeStringUtilities.h:34
size_type count(const key_type &__x) const
Definition: stdcxx/bstdmap.h:277
uint8_t UInt8
Definition: Bentley.r.h:116
wchar_t const * WCharCP
Definition: Bentley.h:224
static void CopyUtf16(Utf16Char(&outStr)[outStrCapacity], Utf16CP inStr)
Copy a 0-terminated Unicode string encoded as UTF-16 into a Utf16 buffer.
Definition: BeStringUtilities.h:141
uint32_t UInt32
Definition: Bentley.r.h:128
#define NULL
Definition: Bentley.h:157
#define BEGIN_BENTLEY_NAMESPACE
Definition: Bentley.r.h:24
static char * Strncpy(char(&destArray)[destArraySize], const char *strSource, size_t count=AsManyAsPossible)
Equivalent to strncpy_s in MSVC, for arrays.
Definition: BeStringUtilities.h:360
Declares the NonCopyableClass.
BentleyStatus BeGetComputerName(WStringR s)
Get the current computer name.
Utf16Buffer const & Utf16BufferCR
Definition: BeStringUtilities.h:29
static BentleyStatus WCharToUtf16(Utf16Char(&ubuf)[ubufSizeInChars], WCharCP inStr, size_t count)
Converts a WChar buffer to a UTF-16 string.
Definition: BeStringUtilities.h:125
unsigned long long uint64_t
Definition: Bentley.r.h:95
BentleyStatus
Definition: Bentley.h:208
Utf16Char * Utf16P
Definition: Bentley.h:230
static void Split(WCharCP str, WCharCP delimiters, bvector< WString > &tokens)
Tokenizes a string based on the provided delimiters, and adds a WString for each token into the provi...
Definition: BeStringUtilities.h:405
static int Snwprintf(wchar_t(&destArray)[destArraySize], WCharCP format,...)
Format a string following the rules of sprintf.
Definition: BeStringUtilities.h:293
Defines typedefs and constants that can be used across other namespaces. All Bentley-authored C++ sou...
LangCodePage
Definition: CodePages.h:20
Base class to make a class non-copyable.
Definition: NonCopyableClass.h:23
static wchar_t * Wcsncpy(wchar_t(&destArray)[destArraySize], const wchar_t *strSource, size_t count=AsManyAsPossible)
Equivalent to wcsncpy_s in MSVC, for arrays.
Definition: BeStringUtilities.h:353
uint64_t UInt64
Definition: Bentley.r.h:131
static void Split(Utf8CP str, Utf8CP delimiters, bvector< Utf8String > &tokens)
Tokenizes a string based on the provided delimiters, and adds a Utf8String for each token into the pr...
Definition: BeStringUtilities.h:424
Utf8Char const * Utf8CP
Definition: Bentley.h:229
char const * CharCP
Definition: Bentley.h:226
#define END_BENTLEY_NAMESPACE
Definition: Bentley.r.h:25
Utf16Buffer & Utf16BufferR
Definition: BeStringUtilities.h:31
A string class that has many of the same capabilities as std::string, plus additional functions such ...
Definition: WString.h:51
Provides a list of language code page value settings (Bentley/CodePages.h).

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.