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 
37 class PAPILLON_API_CORE PString : public PObject
38 {
39 public:
43  enum ECaseSensitivity { E_CASE_INSENSITIVE, E_CASE_SENSITIVE };
44 
48  enum ESplitBehavior { E_KEEP_EMPTY_PARTS, E_SKIP_EMPTY_PARTS };
49 
54  {
57  E_APPEND
58  };
59 
60 public:
64  static const PString& Empty() { static const PString C_EMPTY; return C_EMPTY; }
65 
69  PString();
70 
74  PString(const PString& other);
75 
86  PString(const char* str);
87 
97  PString(const char* str, papillon::int32 n);
98 
108  PString(const wchar_t* str);
109 
117  PString(const std::string& str);
118 
125  PString(const std::wstring& str);
126 
130  PString(const std::u16string& str);
131 
135  explicit PString(char c);
136 
140  explicit PString(papillon::int16 n);
141 
145  explicit PString(papillon::uint16 n);
146 
150  explicit PString(papillon::int32 n);
151 
155  explicit PString(papillon::uint32 n);
156 
160  explicit PString(papillon::int64 n);
161 
165  explicit PString(papillon::uint64 n);
166 
167 #ifndef SWIG
168 # ifdef PAPILLON_INT64C
169 
172  explicit PString(papillon::int64c n);
173 
177  explicit PString(papillon::uint64c n);
178 # endif
179 #endif
180 
195  explicit PString(double n, char format = 'g', papillon::int32 precision = 6);
196 
200  static PString DuplicateChars(papillon::int32 size, papillon::wchar c);
201 
205  virtual ~PString();
206 
213  void Reserve(papillon::int32 size);
214 
218  PString& operator=(const PString& other);
219 
224  papillon::wchar At(papillon::int32 position) const;
225 
229  bool IsEmpty() const;
230 
234  bool NotEmpty() const;
235 
241  bool StartsWith(const PString& s, ECaseSensitivity c = PString::E_CASE_SENSITIVE) const;
242 
248  bool EndsWith(const PString& s, ECaseSensitivity c = PString::E_CASE_SENSITIVE) const;
249 
255  bool Contains(const PString& s, ECaseSensitivity c = PString::E_CASE_SENSITIVE) const;
256 
261  papillon::uint32 Count(const PString& s, ECaseSensitivity c = PString::E_CASE_SENSITIVE) const;
262 
268  papillon::int32 IndexOf(const PString& s, ECaseSensitivity c = PString::E_CASE_SENSITIVE) const;
269 
275  papillon::int32 LastIndexOf(const PString& s, ECaseSensitivity c = PString::E_CASE_SENSITIVE) const;
276 
280  papillon::int32 Size() const;
281 
289  const char* c_str() const;
290 
298  const wchar_t* c_wstr() const;
299 
307  const char* ConstUTF8() const;
308 
309 #ifndef SWIG
310  const papillon::wchar* ConstUTF16() const;
311 #endif
312 
316  const std::string& stdString() const;
317 
321  const std::u16string& u16String() const;
322 
326  papillon::PByteArray ToUTF8() const;
327 
332  static PString FromUTF8(const papillon::PByteArray& utf8Buffer);
333 
337 // PString Latinized() const;
338 
347  PString Substring(papillon::int32 position, papillon::int32 n = -1) const;
348 
353  PString Trim() const;
354 
360  PString& Truncate(papillon::int32 position);
361 
367  PString& Chop(papillon::int32 n);
368 
378  PString RemoveEndDigits() const;
379 
386  PString PadRight(papillon::int32 size, papillon::wchar separator = ' ') const;
387 
391  PString Quote() const;
392 
396  PString Unquote() const;
397 
401  PString DoubleBackslash() const;
402 
406  PString AddLineNumbers(bool columns = false) const;
407 
411  PString ToUpper() const;
412 
416  PString ToLower() const;
417 
426  papillon::PStringList Split(papillon::wchar separator, ESplitBehavior splitBehavior = E_KEEP_EMPTY_PARTS) const;
427 
432  papillon::PStringList Split(const papillon::PString& separator, ESplitBehavior splitBehavior = E_KEEP_EMPTY_PARTS) const;
433 
439  papillon::PStringList SplitSameLength(papillon::int32 size) const;
440 
451  PString Arg(const PString& str, papillon::int32 fieldWidth = 0, papillon::wchar fillChar = ' ') const;
452 
458  PString Arg(void* value) const;
459 
465  PString Arg(const char* value) const;
466 
472  PString Arg(const wchar_t* str) const;
473 
479  PString Arg(bool value) const;
480 
491  PString Arg(papillon::int32 value, papillon::int32 fieldWidth = 0, papillon::int32 base = 10, papillon::wchar fillChar = ' ') const;
492  PString Arg(papillon::int32 value, papillon::int32 fieldWidth, papillon::int32 base, char fillChar) const;
493 
504  PString Arg(papillon::int64 value, papillon::int32 fieldWidth = 0, papillon::int32 base = 10, papillon::wchar fillChar = ' ') const;
505  PString Arg(papillon::int64 value, papillon::int32 fieldWidth, papillon::int32 base, char fillChar) const;
506 
517  PString Arg(papillon::uint32 value, papillon::int32 fieldWidth = 0, papillon::int32 base = 10, papillon::wchar fillChar = ' ') const;
518  PString Arg(papillon::uint32 value, papillon::int32 fieldWidth, papillon::int32 base, char fillChar) const;
519 
530  PString Arg(papillon::uint64 value, papillon::int32 fieldWidth = 0, papillon::int32 base = 10, papillon::wchar fillChar = ' ') const;
531  PString Arg(papillon::uint64 value, papillon::int32 fieldWidth, papillon::int32 base, char fillChar) const;
532 
533 #ifndef SWIG
534 # ifdef PAPILLON_INT64C
535 
545  PString Arg(papillon::int64c value, papillon::int32 fieldWidth = 0, papillon::int32 base = 10, papillon::wchar fillChar = ' ') const;
546 
556  PString Arg(papillon::uint64c value, papillon::int32 fieldWidth = 0, papillon::int32 base = 10, papillon::wchar fillChar = ' ') const;
557 # endif
558 #endif
559 
577  PString Arg(double value, papillon::int32 fieldWidth = 0, char format = 'g', papillon::int32 precision = -1, papillon::wchar fillChar = ' ') const;
578  PString Arg(double value, papillon::int32 fieldWidth, char format, papillon::int32 precision, char fillChar) const;
579 
597  PString Arg(float value, papillon::int32 fieldWidth = 0, char format = 'g', papillon::int32 precision = -1, papillon::wchar fillChar = ' ') const;
598  PString Arg(float value, papillon::int32 fieldWidth, char format, papillon::int32 precision, char fillChar) const;
599 
605  PString Arg(const papillon::PObject& object) const;
606 
612  PString Arg(const papillon::PResult& result) const;
613 
617  PString& operator+=(const PString& other);
618 
622  PString& operator+=(papillon::wchar car);
623 
629  papillon::int32 Compare(const PString& other, ECaseSensitivity caseSensitivity = E_CASE_SENSITIVE) const;
630 
635  bool operator!=(const PString& other) const;
636 
642  bool operator<(const PString& other) const;
643 
649  bool operator<=(const PString& other) const;
650 
655  bool operator==(const PString& other) const;
656 
662  bool operator>(const PString& other) const;
663 
669  bool operator>=(const PString& other) const;
670 
676  bool ToBool(bool* ok = NULL) const;
677 
682  papillon::int32 ToInt32(bool* ok = NULL) const;
683 
688  papillon::uint32 ToUInt32(bool* ok = NULL) const;
689 
694  papillon::int64 ToInt64(bool* ok = NULL) const;
695 
700  papillon::uint64 ToUInt64(bool* ok = NULL) const;
701 
707  float ToFloat(bool* ok = NULL) const;
708 
714  double ToDouble(bool* ok = NULL) const;
715 
720  PString& Replace(const PString& before, const PString& after);
721 
726  PString& Replace(papillon::int32 pos, papillon::int32 n, const PString& after);
727 
734  papillon::PResult Save(const PString& textFilename, EWriteMode mode = E_OVERWRITE) const;
735 
744  papillon::PResult Load(const PString& textFilename);
745 
746 private:
747  void Detach();
748 };
749 
754 PAPILLON_API_CORE papillon::PString operator+(const papillon::PString& lhs, const papillon::PString& rhs);
755 
756 
757 template <typename T> inline PString ToString(const T& t) { return t.ToString(); }
758 template <> inline PString ToString<papillon::int32>(const papillon::int32& t) { return PString(t); }
759 
760 PAPILLON_END_NAMESPACE
The PString class provides an Unicode character strings.
Definition: PString.h:37
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:64
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