PString.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2018 Digital Barriers plc. All rights reserved.
3  * Contact: http://www.digitalbarriers.com/
4  *
5  * This file is part of the Papillon SDK.
6  *
7  * You can't use, modify or distribute any part of this file without
8  * the explicit written agreements of Digital Barriers.
9  */
10 
15 #pragma once
16 #include <PObject.h>
17 #ifndef PAPILLON_WINDOWS
18 #include <cstddef>
19 #include <cstdlib>
20 #endif
21 
22 PAPILLON_BEGIN_NAMESPACE
23 
24 class PByteArray;
25 class PStringList;
26 
38 class PAPILLON_API_CORE PString : public PObject {
39  public:
43  enum ECaseSensitivity { E_CASE_INSENSITIVE, E_CASE_SENSITIVE };
44 
48  enum ESplitBehavior { E_KEEP_EMPTY_PARTS, E_SKIP_EMPTY_PARTS };
49 
53  enum EWriteMode { E_FAIL_IF_EXIST, E_OVERWRITE, E_APPEND };
54 
55  public:
59  static const PString& Empty() {
60  static const PString C_EMPTY;
61  return C_EMPTY;
62  }
63 
67  PString();
68 
72  PString(const PString& other);
73 
84  PString(const char* str);
85 
95  PString(const char* str, papillon::int32 n);
96 
106  PString(const wchar_t* str);
107 
115  PString(const std::string& str);
116 
123  PString(const std::wstring& str);
124 
128  PString(const std::u16string& str);
129 
133  PString(std::u16string&& str);
134 
138  explicit PString(char c);
139 
143  explicit PString(papillon::int16 n);
144 
148  explicit PString(papillon::uint16 n);
149 
153  explicit PString(papillon::int32 n);
154 
158  explicit PString(papillon::uint32 n);
159 
163  explicit PString(papillon::int64 n);
164 
168  explicit PString(papillon::uint64 n);
169 
170 #ifndef SWIG
171 #ifdef PAPILLON_INT64C
172 
175  explicit PString(papillon::int64c n);
176 
180  explicit PString(papillon::uint64c n);
181 #endif
182 #endif
183 
199  explicit PString(double n, char format = 'g', papillon::int32 precision = 6);
200 
204  static PString DuplicateChars(papillon::int32 size, papillon::wchar c);
205 
209  virtual ~PString();
210 
217  void Reserve(papillon::int32 size);
218 
222  PString& operator=(const PString& other);
223 
228  papillon::wchar At(papillon::int32 position) const;
229 
233  bool IsEmpty() const;
234 
238  bool NotEmpty() const;
239 
245  bool StartsWith(const PString& s, ECaseSensitivity c = PString::E_CASE_SENSITIVE) const;
246 
252  bool EndsWith(const PString& s, ECaseSensitivity c = PString::E_CASE_SENSITIVE) const;
253 
259  bool Contains(const PString& s, ECaseSensitivity c = PString::E_CASE_SENSITIVE) const;
260 
265  papillon::uint32 Count(const PString& s, ECaseSensitivity c = PString::E_CASE_SENSITIVE) const;
266 
272  papillon::int32 IndexOf(const PString& s, ECaseSensitivity c = PString::E_CASE_SENSITIVE) const;
273 
279  papillon::int32 LastIndexOf(const PString& s, ECaseSensitivity c = PString::E_CASE_SENSITIVE) const;
280 
284  papillon::int32 Size() const;
285 
293  const char* c_str() const;
294 
302  const wchar_t* c_wstr() const;
303 
311  const char* ConstUTF8() const;
312 
313 #ifndef SWIG
314  const papillon::wchar* ConstUTF16() const;
315 #endif
316 
320  const std::string& stdString() const;
321 
325  const std::u16string& u16String() const;
326 
331  papillon::PByteArray ToUTF8() const;
332 
337  static PString FromUTF8(const papillon::PByteArray& utf8Buffer);
338 
342  // PString Latinized() const;
343 
352  PString Substring(papillon::int32 position, papillon::int32 n = -1) const;
353 
358  PString Trim() const;
359 
365  PString& Truncate(papillon::int32 position);
366 
372  PString& Chop(papillon::int32 n);
373 
383  PString RemoveEndDigits() const;
384 
391  PString PadRight(papillon::int32 size, papillon::wchar separator = ' ') const;
392 
396  PString Quote() const;
397 
401  PString Unquote() const;
402 
406  PString DoubleBackslash() const;
407 
411  PString AddLineNumbers(bool columns = false) const;
412 
416  PString ToUpper() const;
417 
421  PString ToLower() const;
422 
431  papillon::PStringList Split(papillon::wchar separator, ESplitBehavior splitBehavior = E_KEEP_EMPTY_PARTS) const;
432 
437  papillon::PStringList Split(const papillon::PString& separator,
438  ESplitBehavior splitBehavior = E_KEEP_EMPTY_PARTS) const;
439 
445  papillon::PStringList SplitSameLength(papillon::int32 size) const;
446 
457  PString Arg(const PString& str, papillon::int32 fieldWidth = 0, papillon::wchar fillChar = ' ') const;
458 
464  PString Arg(void* value) const;
465 
471  PString Arg(const char* value) const;
472 
478  PString Arg(const wchar_t* str) const;
479 
485  PString Arg(bool value) const;
486 
497  PString Arg(papillon::int32 value,
498  papillon::int32 fieldWidth = 0,
499  papillon::int32 base = 10,
500  papillon::wchar fillChar = ' ') const;
501  PString Arg(papillon::int32 value, papillon::int32 fieldWidth, papillon::int32 base, char fillChar) const;
502 
513  PString Arg(papillon::int64 value,
514  papillon::int32 fieldWidth = 0,
515  papillon::int32 base = 10,
516  papillon::wchar fillChar = ' ') const;
517  PString Arg(papillon::int64 value, papillon::int32 fieldWidth, papillon::int32 base, char fillChar) const;
518 
529  PString Arg(papillon::uint32 value,
530  papillon::int32 fieldWidth = 0,
531  papillon::int32 base = 10,
532  papillon::wchar fillChar = ' ') const;
533  PString Arg(papillon::uint32 value, papillon::int32 fieldWidth, papillon::int32 base, char fillChar) const;
534 
545  PString Arg(papillon::uint64 value,
546  papillon::int32 fieldWidth = 0,
547  papillon::int32 base = 10,
548  papillon::wchar fillChar = ' ') const;
549  PString Arg(papillon::uint64 value, papillon::int32 fieldWidth, papillon::int32 base, char fillChar) const;
550 
551 #ifndef SWIG
552 #ifdef PAPILLON_INT64C
553 
563  PString Arg(papillon::int64c value,
564  papillon::int32 fieldWidth = 0,
565  papillon::int32 base = 10,
566  papillon::wchar fillChar = ' ') const;
567 
577  PString Arg(papillon::uint64c value,
578  papillon::int32 fieldWidth = 0,
579  papillon::int32 base = 10,
580  papillon::wchar fillChar = ' ') const;
581 #endif
582 #endif
583 
601  PString Arg(double value,
602  papillon::int32 fieldWidth = 0,
603  char format = 'g',
604  papillon::int32 precision = -1,
605  papillon::wchar fillChar = ' ') const;
606  PString Arg(double value, papillon::int32 fieldWidth, char format, papillon::int32 precision, char fillChar) const;
607 
625  PString Arg(float value,
626  papillon::int32 fieldWidth = 0,
627  char format = 'g',
628  papillon::int32 precision = -1,
629  papillon::wchar fillChar = ' ') const;
630  PString Arg(float value, papillon::int32 fieldWidth, char format, papillon::int32 precision, char fillChar) const;
631 
637  PString Arg(const papillon::PObject& object) const;
638 
644  PString Arg(const papillon::PResult& result) const;
645 
649  PString& operator+=(const PString& other);
650 
654  PString& operator+=(papillon::wchar car);
655 
661  papillon::int32 Compare(const PString& other, ECaseSensitivity caseSensitivity = E_CASE_SENSITIVE) const;
662 
667  bool operator!=(const PString& other) const;
668 
674  bool operator<(const PString& other) const;
675 
681  bool operator<=(const PString& other) const;
682 
687  bool operator==(const PString& other) const;
688 
694  bool operator>(const PString& other) const;
695 
701  bool operator>=(const PString& other) const;
702 
708  bool ToBool(bool* ok = NULL) const;
709 
714  papillon::int32 ToInt32(bool* ok = NULL) const;
715 
720  papillon::uint32 ToUInt32(bool* ok = NULL) const;
721 
726  papillon::int64 ToInt64(bool* ok = NULL) const;
727 
732  papillon::uint64 ToUInt64(bool* ok = NULL) const;
733 
739  float ToFloat(bool* ok = NULL) const;
740 
746  double ToDouble(bool* ok = NULL) const;
747 
752  PString& Replace(const PString& before, const PString& after);
753 
758  PString& Replace(papillon::int32 pos, papillon::int32 n, const PString& after);
759 
766  papillon::PResult Save(const PString& textFilename, EWriteMode mode = E_OVERWRITE) const;
767 
776  papillon::PResult Load(const PString& textFilename);
777 
778  private:
779  void Detach();
780 };
781 
785 PAPILLON_API_CORE papillon::PString operator+(const papillon::PString& lhs, const papillon::PString& rhs);
786 
787 template <typename T>
788 inline PString ToString(const T& t) {
789  return t.ToString();
790 }
791 template <>
792 inline PString ToString<papillon::int32>(const papillon::int32& t) {
793  return PString(t);
794 }
795 
796 PAPILLON_END_NAMESPACE
The PString class provides an Unicode character strings.
Definition: PString.h:38
ECaseSensitivity
Used to change case sensitive behavior of some methods.
Definition: PString.h:43
PAPILLON_API_CORE papillon::PString operator+(const papillon::PString &lhs, const papillon::PString &rhs)
Constructs a new string by concatenating string lhs with string rhs.
EWriteMode
All the available write mode.
Definition: PString.h:53
The PByteArray class represents a resizable array of bytes.
Definition: PByteArray.h:40
ESplitBehavior
Used to control the behavior of Split().
Definition: PString.h:48
The PObject class is the root class of all Papillon classes; all PObject behaves like shared referenc...
Definition: PObject.h:27
PString ToString() const
Returns a string representation of this object.
The PStringList class represents a list of PString; NOT thread-safe.
Definition: PStringList.h:26
static const PString & Empty()
Returns the constant holding the empty string "".
Definition: PString.h:59
Class PObject: the root class of all Papillon classes.
PObject & operator=(const PObject &obj)
Performs a shared copy of other to this object.
Definition: PObject.h:45