File: StringHelpers.h

package info (click to toggle)
pcsx2 1.6.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 28,464 kB
  • sloc: cpp: 299,796; ansic: 23,973; lisp: 2,689; asm: 908; perl: 852; sh: 789; xml: 116; makefile: 60
file content (235 lines) | stat: -rw-r--r-- 8,157 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*  PCSX2 - PS2 Emulator for PCs
 *  Copyright (C) 2002-2010  PCSX2 Dev Team
 *
 *  PCSX2 is free software: you can redistribute it and/or modify it under the terms
 *  of the GNU Lesser General Public License as published by the Free Software Found-
 *  ation, either version 3 of the License, or (at your option) any later version.
 *
 *  PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 *  PURPOSE.  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along with PCSX2.
 *  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "Dependencies.h"
#include "SafeArray.h"
#include "ScopedAlloc.h"

#include <wx/tokenzr.h>

#if _WIN32
#define WX_STR(str) (str.wc_str())
#else
// Stupid wx3.0 doesn't support c_str for vararg function
#define WX_STR(str) (static_cast<const char *>(str.c_str()))
#endif

// --------------------------------------------------------------------------------------
//  pxToUTF8
// --------------------------------------------------------------------------------------
// Converts a string to UTF8 and provides an interface for getting its length.
class pxToUTF8
{
    DeclareNoncopyableObject(pxToUTF8);

protected:
    wxCharBuffer m_result;
    int m_length;

public:
    explicit pxToUTF8(const wxString &src)
        : m_result(src.ToUTF8())
    {
        m_length = -1;
    }

    size_t Length()
    {
        if (-1 == m_length)
            m_length = strlen(m_result);
        return m_length;
    }

    void Convert(const wxString &src)
    {
        m_result = src.ToUTF8();
        m_length = -1;
    }

    const char *data() const { return m_result; }

    operator const char *() const
    {
        return m_result.data();
    }
};

extern void px_fputs(FILE *fp, const char *src);

// wxWidgets lacks one of its own...
extern const wxRect wxDefaultRect;

extern void SplitString(wxArrayString &dest, const wxString &src, const wxString &delims, wxStringTokenizerMode mode = wxTOKEN_RET_EMPTY_ALL);
extern wxString JoinString(const wxArrayString &src, const wxString &separator);
extern wxString JoinString(const wxChar **src, const wxString &separator);

extern wxString ToString(const wxPoint &src, const wxString &separator = L",");
extern wxString ToString(const wxSize &src, const wxString &separator = L",");
extern wxString ToString(const wxRect &src, const wxString &separator = L",");

extern bool TryParse(wxPoint &dest, const wxStringTokenizer &parts);
extern bool TryParse(wxSize &dest, const wxStringTokenizer &parts);

extern bool TryParse(wxPoint &dest, const wxString &src, const wxPoint &defval = wxDefaultPosition, const wxString &separators = L",");
extern bool TryParse(wxSize &dest, const wxString &src, const wxSize &defval = wxDefaultSize, const wxString &separators = L",");
extern bool TryParse(wxRect &dest, const wxString &src, const wxRect &defval = wxDefaultRect, const wxString &separators = L",");

// --------------------------------------------------------------------------------------
//  ParsedAssignmentString
// --------------------------------------------------------------------------------------
// This class is a simple helper for parsing INI-style assignments, in the typical form of:
//    variable = value
//    filename = SomeString.txt
//    integer  = 15
//
// This parser supports both '//' and ';' at the head of a line as indicators of a commented
// line, and such a line will return empty strings for l- and r-value.
//
// No type handling is performed -- the user must manually parse strings into integers, etc.
// For advanced "fully functional" ini file parsing, consider using wxFileConfig instead.
//
struct ParsedAssignmentString
{
    wxString lvalue;
    wxString rvalue;
    bool IsComment;

    ParsedAssignmentString(const wxString &src);
};

// ======================================================================================
//  FastFormatAscii / FastFormatUnicode  (overview!)
// ======================================================================================
// Fast formatting of ASCII or Unicode text.  These classes uses a series of thread-local
// format buffers that are allocated only once and grown to accommodate string formatting
// needs.  Because the buffers are thread-local, no thread synch objects are required in
// order to format strings, allowing for multi-threaded string formatting operations to be
// performed with maximum efficiency.  This class also reduces the overhead typically required
// to allocate string buffers off the heap.
//
// Drawbacks:
//  * Some overhead is added to the creation and destruction of threads, however since thread
//    construction is a typically slow process, and often avoided to begin with, this should
//    be a sound trade-off.
//
// Notes:
//  * conversion to wxString requires a heap allocation.
//  * FastFormatUnicode can accept either UTF8 or UTF16/32 (wchar_t) input, but FastFormatAscii
//    accepts Ascii/UTF8 only.
//

typedef ScopedAlignedAlloc<char, 16> CharBufferType;
// --------------------------------------------------------------------------------------
//  FastFormatAscii
// --------------------------------------------------------------------------------------

class FastFormatAscii
{
protected:
    CharBufferType m_dest;

public:
    FastFormatAscii();
    ~FastFormatAscii() = default;
    FastFormatAscii &Write(const char *fmt, ...);
    FastFormatAscii &WriteV(const char *fmt, va_list argptr);

    void Clear();
    bool IsEmpty() const;

    const char *c_str() const { return m_dest.GetPtr(); }
    operator const char *() const { return m_dest.GetPtr(); }

    const wxString GetString() const;
    //operator wxString() const;

    FastFormatAscii &operator+=(const wxString &s)
    {
        Write("%s", WX_STR(s));
        return *this;
    }

    FastFormatAscii &operator+=(const wxChar *psz)
    {
        Write("%ls", psz);
        return *this;
    }

    FastFormatAscii &operator+=(const char *psz)
    {
        Write("%s", psz);
        return *this;
    }
};

// --------------------------------------------------------------------------------------
//  FastFormatUnicode
// --------------------------------------------------------------------------------------
class FastFormatUnicode
{
protected:
    CharBufferType m_dest;
    uint m_Length;

public:
    FastFormatUnicode();
    ~FastFormatUnicode() = default;

    FastFormatUnicode &Write(const char *fmt, ...);
    FastFormatUnicode &Write(const wxChar *fmt, ...);
    FastFormatUnicode &Write(const wxString fmt, ...);
    FastFormatUnicode &WriteV(const char *fmt, va_list argptr);
    FastFormatUnicode &WriteV(const wxChar *fmt, va_list argptr);

    void Clear();
    bool IsEmpty() const;
    uint Length() const { return m_Length; }

    FastFormatUnicode &ToUpper();
    FastFormatUnicode &ToLower();

    const wxChar *c_str() const { return (const wxChar *)m_dest.GetPtr(); }
    operator const wxChar *() const { return (const wxChar *)m_dest.GetPtr(); }
    operator wxString() const { return (const wxChar *)m_dest.GetPtr(); }

    FastFormatUnicode &operator+=(const wxString &s)
    {
        Write(L"%s", WX_STR(s));
        return *this;
    }

    FastFormatUnicode &operator+=(const wxChar *psz)
    {
        Write(L"%s", psz);
        return *this;
    }

    FastFormatUnicode &operator+=(const char *psz);
};

extern bool pxParseAssignmentString(const wxString &src, wxString &ldest, wxString &rdest);

#define pxsFmt FastFormatUnicode().Write
#define pxsFmtV FastFormatUnicode().WriteV

#define pxsPtr(ptr) pxsFmt("0x%08X", (ptr)).c_str()

extern wxString &operator+=(wxString &str1, const FastFormatUnicode &str2);
extern wxString operator+(const wxString &str1, const FastFormatUnicode &str2);
extern wxString operator+(const wxChar *str1, const FastFormatUnicode &str2);
extern wxString operator+(const FastFormatUnicode &str1, const wxString &str2);
extern wxString operator+(const FastFormatUnicode &str1, const wxChar *str2);