| 1 |
/*****************************************************************************/ |
| 2 |
// Copyright 2006-2020 Adobe Systems Incorporated |
| 3 |
// All Rights Reserved. |
| 4 |
// |
| 5 |
// NOTICE: Adobe permits you to use, modify, and distribute this file in |
| 6 |
// accordance with the terms of the Adobe license agreement accompanying it. |
| 7 |
/*****************************************************************************/ |
| 8 |
|
| 9 |
/** \file |
| 10 |
* Text string representation. |
| 11 |
*/ |
| 12 |
|
| 13 |
/*****************************************************************************/ |
| 14 |
|
| 15 |
#ifndef __dng_string__ |
| 16 |
#define __dng_string__ |
| 17 |
|
| 18 |
/*****************************************************************************/ |
| 19 |
|
| 20 |
#include "dng_types.h" |
| 21 |
#include "dng_memory.h" |
| 22 |
|
| 23 |
#include <memory> |
| 24 |
#include <string> |
| 25 |
#include <unordered_set> |
| 26 |
|
| 27 |
/*****************************************************************************/ |
| 28 |
|
| 29 |
typedef std::basic_string<char, |
| 30 |
std::char_traits<char>, |
| 31 |
dng_std_allocator<char>> dng_std_string; |
| 32 |
|
| 33 |
/*****************************************************************************/ |
| 34 |
|
| 35 |
class dng_string |
| 36 |
{ |
| 37 |
|
| 38 |
private: |
| 39 |
|
| 40 |
// Strings are always stored using UTF-8 encoding. Shared pointers |
| 41 |
// are used to provide copy-on-write optimizations. Null strings |
| 42 |
// are always encoded as a null shared pointer (not pointer to a |
| 43 |
// null string). |
| 44 |
|
| 45 |
std::shared_ptr<const dng_std_string> fData; |
| 46 |
|
| 47 |
public: |
| 48 |
|
| 49 |
dng_string (); |
| 50 |
|
| 51 |
dng_string (const dng_string &s); |
| 52 |
|
| 53 |
dng_string (const char *s); |
| 54 |
|
| 55 |
dng_string & operator= (const dng_string &s); |
| 56 |
|
| 57 |
~dng_string (); |
| 58 |
|
| 59 |
const char * Get () const; |
| 60 |
|
| 61 |
bool IsASCII () const; |
| 62 |
|
| 63 |
void Set (const char *s); |
| 64 |
|
| 65 |
void Set_ASCII (const char *s); |
| 66 |
|
| 67 |
void Set_UTF8 (const char *s); |
| 68 |
|
| 69 |
uint32 Get_SystemEncoding (dng_memory_data &buffer) const; |
| 70 |
|
| 71 |
void Set_SystemEncoding (const char *s); |
| 72 |
|
| 73 |
bool ValidSystemEncoding () const; |
| 74 |
|
| 75 |
void Set_JIS_X208_1990 (const char *s); |
| 76 |
|
| 77 |
static uint32 DecodeUTF8 (const char *&s, |
| 78 |
uint32 maxBytes = 6, |
| 79 |
bool *isValid = NULL); |
| 80 |
|
| 81 |
static bool IsUTF8 (const char *s); |
| 82 |
|
| 83 |
void Set_UTF8_or_System (const char *s); |
| 84 |
|
| 85 |
uint32 Get_UTF16 (dng_memory_data &buffer) const; |
| 86 |
|
| 87 |
void Set_UTF16 (const uint16 *s); |
| 88 |
|
| 89 |
void Clear (); |
| 90 |
|
| 91 |
void Truncate (uint32 maxBytes); |
| 92 |
|
| 93 |
bool TrimTrailingBlanks (); |
| 94 |
|
| 95 |
bool TrimLeadingBlanks (); |
| 96 |
|
| 97 |
bool IsEmpty () const; |
| 98 |
|
| 99 |
bool NotEmpty () const |
| 100 |
{ |
| 101 |
return !IsEmpty (); |
| 102 |
} |
| 103 |
|
| 104 |
uint32 Length () const; |
| 105 |
|
| 106 |
bool operator== (const dng_string &s) const; |
| 107 |
|
| 108 |
bool operator!= (const dng_string &s) const |
| 109 |
{ |
| 110 |
return !(*this == s); |
| 111 |
} |
| 112 |
|
| 113 |
// A utility for doing case insensitive comparisons on strings... |
| 114 |
|
| 115 |
static bool Matches (const char *t, |
| 116 |
const char *s, |
| 117 |
bool case_sensitive = false); |
| 118 |
|
| 119 |
// ...wrapped up for use with dng_string. |
| 120 |
|
| 121 |
bool Matches (const char *s, |
| 122 |
bool case_sensitive = false) const; |
| 123 |
|
| 124 |
bool StartsWith (const char *s, |
| 125 |
bool case_sensitive = false) const; |
| 126 |
|
| 127 |
bool EndsWith (const char *s, |
| 128 |
bool case_sensitive = false) const; |
| 129 |
|
| 130 |
bool Contains (const char *s, |
| 131 |
bool case_sensitive = false, |
| 132 |
int32 *match_offset = NULL) const; |
| 133 |
|
| 134 |
bool Replace (const char *old_string, |
| 135 |
const char *new_string, |
| 136 |
bool case_sensitive = true); |
| 137 |
|
| 138 |
void ReplaceChars (char oldChar, |
| 139 |
char newChar); |
| 140 |
|
| 141 |
bool TrimLeading (const char *s, |
| 142 |
bool case_sensitive = false); |
| 143 |
|
| 144 |
void Append (const char *s); |
| 145 |
|
| 146 |
void SetUppercase (); |
| 147 |
|
| 148 |
void SetLowercase (); |
| 149 |
|
| 150 |
void SetLineEndings (char ending); |
| 151 |
|
| 152 |
void SetLineEndingsToNewLines () |
| 153 |
{ |
| 154 |
SetLineEndings ('\n'); |
| 155 |
} |
| 156 |
|
| 157 |
void SetLineEndingsToReturns () |
| 158 |
{ |
| 159 |
SetLineEndings ('\r'); |
| 160 |
} |
| 161 |
|
| 162 |
void StripLowASCII (); |
| 163 |
|
| 164 |
void ForceASCII (); |
| 165 |
|
| 166 |
int32 Compare (const dng_string &s, |
| 167 |
bool digitsAsNumber = true) const; |
| 168 |
|
| 169 |
// A utility to convert fields of numbers into comma separated numbers. |
| 170 |
|
| 171 |
void NormalizeAsCommaSeparatedNumbers (); |
| 172 |
|
| 173 |
}; |
| 174 |
|
| 175 |
/*****************************************************************************/ |
| 176 |
|
| 177 |
class dng_string_hash |
| 178 |
{ |
| 179 |
|
| 180 |
public: |
| 181 |
|
| 182 |
size_t operator () (const dng_string &s) const; |
| 183 |
|
| 184 |
}; |
| 185 |
|
| 186 |
/*****************************************************************************/ |
| 187 |
|
| 188 |
typedef std::unordered_set<dng_string, |
| 189 |
dng_string_hash> dng_string_table; |
| 190 |
|
| 191 |
/*****************************************************************************/ |
| 192 |
|
| 193 |
#endif // __dng_string__ |
| 194 |
|
| 195 |
/*****************************************************************************/ |