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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
/*
FARString.hpp
Unicode строки
*/
/*
Copyright (c) 1996 Eugene Roshal
Copyright (c) 2000 Far Group
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "headers.hpp"
#include <StackHeapArray.hpp>
#include <stdarg.h>
#include <assert.h>
#include <limits>
#include "lang.hpp"
/// change to 1 to enable stats & leaks detection (slow!)
#if 0
#include <mutex>
#include <set>
struct DbgStr
{
std::mutex Mtx;
unsigned long long Addrefs = 0;
std::set<void *> Instances;
};
static DbgStr &DBGSTR()
{
static DbgStr s_out;
return s_out;
}
void FN_NOINLINE dbgStrCreated(void *c, unsigned int Capacity)
{
std::lock_guard<std::mutex> lock(DBGSTR().Mtx);
if (!DBGSTR().Instances.insert(c).second) abort();
// fprintf(stderr, "dbgStrCreated: Instances=%lu Addrefs=%llu [%u]\n",
// (unsigned long)DBGSTR().Instances.size(), DBGSTR().Addrefs, Capacity);
}
void FN_NOINLINE dbgStrDestroyed(void *c, unsigned int Capacity)
{
std::lock_guard<std::mutex> lock(DBGSTR().Mtx);
if (!DBGSTR().Instances.erase(c)) abort();
// fprintf(stderr, "dbgStrDestroyed: Instances=%lu Addrefs=%llu [%u]\n",
// (unsigned long)DBGSTR().Instances.size(), DBGSTR().Addrefs, Capacity);
}
void FN_NOINLINE dbgStrAddref(const wchar_t *Data)
{
std::lock_guard<std::mutex> lock(DBGSTR().Mtx);
++DBGSTR().Addrefs;
// fprintf(stderr, "dbgStrAddref: Instances=%lu Addrefs=%llu '%ls'\n",
// (unsigned long)DBGSTR().Instances.size(), DBGSTR().Addrefs, Data);
}
static std::set<void *> s_PrevStrings;
void FARString::ScanForLeaks()
{
std::lock_guard<std::mutex> lock(DBGSTR().Mtx);
const auto &strs = DBGSTR().Instances;
fprintf(stderr, "========= %s: Count=%lu Addrefs=%llu\n",
__FUNCTION__, (unsigned long)strs.size(), DBGSTR().Addrefs);
// fprintf(stderr, "dbgStrAddref: Instances=%lu Addrefs=%llu '%ls'\n",
// (unsigned long)DBGSTR().Instances.size(), DBGSTR().Addrefs, Data);
if (!s_PrevStrings.empty())
{
for (const auto &p : strs) if (s_PrevStrings.find(p) == s_PrevStrings.end())
{
FARString::Content *c = (FARString::Content *)p;
fprintf(stderr, " + refs=%u cap=%u len=%u data='%ls'\n",
c->GetRefs(), (unsigned int)c->GetCapacity(), (unsigned int)c->GetLength(), c->GetData());
}
}
s_PrevStrings = strs;
}
#else
# define dbgStrCreated(c, Capacity)
# define dbgStrDestroyed(c, Capacity)
# define dbgStrAddref(Data)
void FARString::ScanForLeaks() { }
#endif
FARString::Content FARString::Content::sEmptyData{}; // all fields zero inited by loader
FARString::Content *FARString::Content::Create(size_t nCapacity)
{
if (UNLIKELY(!nCapacity))
return EmptySingleton();
Content *out = (Content *)malloc(sizeof(Content) + sizeof(wchar_t) * nCapacity);
//Так как ни где выше в коде мы не готовы на случай что памяти не хватит
//то уж лучше и здесь не проверять, а сразу падать
out->m_nRefCount = 1;
out->m_nCapacity = (unsigned int)std::min(nCapacity, (size_t)std::numeric_limits<unsigned int>::max());
out->m_nLength = 0;
out->m_Data[0] = 0;
dbgStrCreated(out, out->m_nCapacity);
return out;
}
FARString::Content *FARString::Content::Create(size_t nCapacity, const wchar_t *SrcData, size_t SrcLen)
{
Content *out = Content::Create(nCapacity);
out->m_nLength = (unsigned int)std::min(std::min(nCapacity, SrcLen), (size_t)std::numeric_limits<unsigned int>::max());
wmemcpy(out->m_Data, SrcData, out->m_nLength);
out->m_Data[out->m_nLength] = 0;
return out;
}
void FN_NOINLINE FARString::Content::Destroy(Content *c)
{
dbgStrDestroyed(c, c->m_nCapacity);
if (LIKELY(c != EmptySingleton()))
free(c);
else
abort();
}
void FARString::Content::AddRef()
{
if (LIKELY(m_nCapacity)) // only empty singletone has zero capacity
{
dbgStrAddref(m_Data);
__atomic_add_fetch(&m_nRefCount, 1, __ATOMIC_RELAXED);
}
}
void FARString::Content::DecRef()
{
// __atomic_load_n(relaxed) usually doesn't use any HW interlocking
// thus its a fast path for empty or single-owner strings
unsigned int n = __atomic_load_n(&m_nRefCount, __ATOMIC_RELAXED);
if (LIKELY(n == 0))
{ // (only) empty singletone has always-zero m_nRefCount
return;
}
if (LIKELY(n != 1))
{
if (LIKELY(__atomic_sub_fetch(&m_nRefCount, 1, __ATOMIC_RELEASE) != 0))
return;
}
Destroy(this);
}
void FARString::Content::SetLength(size_t nLength)
{
m_nLength = std::min(nLength, (size_t)m_nCapacity);
m_Data[m_nLength] = 0;
}
//////////////////////////
void FARString::PrepareForModify(size_t nCapacity)
{
if (m_pContent->GetRefs() != 1 || nCapacity > m_pContent->GetCapacity())
{
size_t NewCapacity = nCapacity;
if (m_pContent->GetCapacity() > 0 && NewCapacity > m_pContent->GetCapacity())
NewCapacity+= std::max(NewCapacity - m_pContent->GetCapacity(), (size_t)16);
Content *pNewData =
Content::Create(NewCapacity, m_pContent->GetData(), m_pContent->GetLength());
pNewData->SetLength(m_pContent->GetLength());
m_pContent->DecRef();
m_pContent = pNewData;
}
}
void FARString::PrepareForModify()
{
// invoke PrepareForModify with current length but not capacity intentially
// so in case of single owner this will not alter things but in case of
// not single owner then new copy will be alloced with capacity == length
PrepareForModify(m_pContent->GetLength());
}
size_t FARString::GetCharString(char *lpszStr, size_t nSize, UINT CodePage) const
{
if (!lpszStr || !nSize)
return 0;
int SrcLen;
for (SrcLen = (int)m_pContent->GetLength(); SrcLen; --SrcLen)
{
int DstLen = WINPORT(WideCharToMultiByte)(CodePage, 0,
m_pContent->GetData(), SrcLen, nullptr, 0, nullptr, nullptr);
if (DstLen < (int)nSize)
{
DstLen = WINPORT(WideCharToMultiByte)(CodePage, 0,
m_pContent->GetData(), SrcLen, lpszStr, (int)nSize, nullptr, nullptr);
if (DstLen >= 0 && DstLen < (int)nSize)
{
lpszStr[DstLen] = 0;
return DstLen + 1;
}
}
}
*lpszStr = 0;
return 1;
}
FARString& FARString::Replace(size_t Pos, size_t Len, const wchar_t* Data, size_t DataLen)
{
// Pos & Len must be valid
assert(Pos <= m_pContent->GetLength());
assert(Len <= m_pContent->GetLength());
assert(Pos + Len <= m_pContent->GetLength());
// Data and *this must not intersect (but Data can be located entirely within *this)
const bool DataStartsInside = (Data >= CPtr() && Data < CEnd());
const bool DataEndsInside = (Data + DataLen > CPtr() && Data + DataLen <= CEnd());
assert(DataStartsInside == DataEndsInside);
if (!Len && !DataLen)
return *this;
const size_t NewLength = m_pContent->GetLength() + DataLen - Len;
if (!DataStartsInside && m_pContent->GetRefs() == 1 && NewLength <= m_pContent->GetCapacity())
{
wmemmove(m_pContent->GetData() + Pos + DataLen,
m_pContent->GetData() + Pos + Len, m_pContent->GetLength() - Pos - Len);
wmemcpy(m_pContent->GetData() + Pos, Data, DataLen);
}
else
{
Content *NewData = Content::Create(NewLength);
wmemcpy(NewData->GetData(), m_pContent->GetData(), Pos);
wmemcpy(NewData->GetData() + Pos, Data, DataLen);
wmemcpy(NewData->GetData() + Pos + DataLen, m_pContent->GetData() + Pos + Len, m_pContent->GetLength() - Pos - Len);
m_pContent->DecRef();
m_pContent = NewData;
}
m_pContent->SetLength(NewLength);
return *this;
}
FARString& FARString::Append(const char *lpszAdd, UINT CodePage)
{
if (lpszAdd && *lpszAdd)
{
int nAddSize = WINPORT(MultiByteToWideChar)(CodePage,0,lpszAdd,-1,nullptr,0);
if (nAddSize > 0) {
size_t nNewLength = m_pContent->GetLength() + nAddSize - 1; // minus NUL char that implicitely there
PrepareForModify(nNewLength);
WINPORT(MultiByteToWideChar)(CodePage, 0, lpszAdd, -1, m_pContent->GetData() + m_pContent->GetLength(), nAddSize);
m_pContent->SetLength(nNewLength);
}
}
return *this;
}
FARString& FARString::Append(wchar_t Ch, size_t Count)
{
size_t nNewLength = m_pContent->GetLength() + Count;
PrepareForModify(nNewLength);
wmemset(m_pContent->GetData() + m_pContent->GetLength(), Ch, Count);
m_pContent->SetLength(nNewLength);
return *this;
}
FARString& FARString::Insert(size_t Pos, wchar_t Ch, size_t Count)
{
if (LIKELY(Pos <= m_pContent->GetLength())) {
size_t nNewLength = m_pContent->GetLength() + Count;
PrepareForModify(nNewLength);
wmemmove(m_pContent->GetData() + Pos + Count, m_pContent->GetData() + Pos, m_pContent->GetLength() - Pos);
wmemset(m_pContent->GetData() + Pos, Ch, Count);
m_pContent->SetLength(nNewLength);
}
return *this;
}
FARString& FARString::Copy(const FARString &Str)
{
auto prev_pContent = m_pContent;
m_pContent = Str.m_pContent;
m_pContent->AddRef();
prev_pContent->DecRef();
return *this;
}
FARString& FARString::Copy(const char *lpszData, UINT CodePage)
{
m_pContent->DecRef();
if (!lpszData || !*lpszData)
{
Init();
}
else
{
int nSize = WINPORT(MultiByteToWideChar)(CodePage,0,lpszData,-1,nullptr,0);
if (nSize > 0) {
m_pContent = Content::Create(nSize - 1);
WINPORT(MultiByteToWideChar)(CodePage, 0, lpszData, -1, m_pContent->GetData(), nSize);
m_pContent->SetLength(nSize - 1);
} else
Init();
}
return *this;
}
FARString FARString::SubStr(size_t Pos, size_t Len)
{
if (Pos >= GetLength())
return FARString();
if (Len == static_cast<size_t>(-1) || Len > GetLength() || Pos + Len > GetLength())
Len = GetLength() - Pos;
return FARString(m_pContent->GetData() + Pos, Len);
}
bool FARString::Equal(size_t Pos, size_t Len, const wchar_t* Data, size_t DataLen) const
{
if (Pos >= m_pContent->GetLength())
Len = 0;
else if (Len >= m_pContent->GetLength() || Pos + Len >= m_pContent->GetLength())
Len = m_pContent->GetLength() - Pos;
if (Len != DataLen)
return false;
return !wmemcmp(m_pContent->GetData() + Pos, Data, Len);
}
bool FARString::operator<(const FARString& Str) const
{
return wmemcmp(CPtr(), Str.CPtr(), std::min(GetLength(), Str.GetLength()) + 1 ) < 0;
}
FARString operator+(const FARString &strSrc1, const FARString &strSrc2)
{
FARString out = strSrc1.Clone();
out.Append(strSrc2);
return out;
}
FARString operator+(const FARString &strSrc1, const char *lpszSrc2)
{
FARString out = strSrc1.Clone();
out.Append(lpszSrc2);
return out;
}
FARString operator+(const FARString &strSrc1, const wchar_t *lpwszSrc2)
{
FARString out = strSrc1.Clone();
out.Append(lpwszSrc2);
return out;
}
wchar_t *FARString::GetBuffer(size_t nLength)
{
if (nLength == (size_t)-1)
nLength = m_pContent->GetLength();
PrepareForModify(nLength);
return m_pContent->GetData();
}
void FARString::ReleaseBuffer(size_t nLength)
{
if (nLength == (size_t)-1)
nLength = wcsnlen(m_pContent->GetData(), m_pContent->GetCapacity());
else if (nLength > m_pContent->GetCapacity())
nLength = m_pContent->GetCapacity();
PrepareForModify(nLength);
m_pContent->SetLength(nLength);
}
size_t FARString::Truncate(size_t nLength)
{
if (nLength < m_pContent->GetLength())
{
if (!nLength)
{
m_pContent->DecRef();
Init();
}
else
{
PrepareForModify(nLength);
m_pContent->SetLength(nLength);
}
}
return m_pContent->GetLength();
}
FARString& FARString::Clear()
{
Truncate(0);
return *this;
}
static void FARStringFmt(FARString &str, bool append, const wchar_t *format, va_list argptr)
{
const size_t StartSize = 0x200; // 512 bytes ought to be enough for mosts
const size_t MaxSize = 0x1000000; // 16 megs ought to be enough for anybody
size_t Size;
for (Size = StartSize; Size <= MaxSize; Size<<= 1)
{
StackHeapArray<wchar_t, StartSize> buf(Size);
if (UNLIKELY(!buf.Get()))
break;
va_list argptr_copy;
va_copy(argptr_copy, argptr);
int retValue = vswprintf(buf.Get(), Size, format, argptr_copy);
va_end(argptr_copy);
if (retValue >= 0 && size_t(retValue) < Size)
{
if (append)
str.Append(buf.Get(), retValue);
else
str.Copy(buf.Get(), retValue);
return;
}
}
fprintf(stderr, "%s: failed; Size=%lu format='%ls'\n", __FUNCTION__, (unsigned long)Size, format);
}
FARString& FARString::Format(const wchar_t * format, ...)
{
va_list argptr;
va_start(argptr, format);
FARStringFmt(*this, false, format, argptr);
va_end(argptr);
return *this;
}
FARString& FARString::AppendFormat(const wchar_t * format, ...)
{
va_list argptr;
va_start(argptr, format);
FARStringFmt(*this, true, format, argptr);
va_end(argptr);
return *this;
}
FARString& FARString::Lower(size_t nStartPos, size_t nLength)
{
PrepareForModify();
WINPORT(CharLowerBuff)(m_pContent->GetData() + nStartPos,
DWORD((nLength == (size_t)-1) ? m_pContent->GetLength() - nStartPos : nLength));
return *this;
}
FARString& FARString::Upper(size_t nStartPos, size_t nLength)
{
PrepareForModify();
WINPORT(CharUpperBuff)(m_pContent->GetData() + nStartPos,
DWORD((nLength == (size_t)-1) ? m_pContent->GetLength() - nStartPos : nLength));
return *this;
}
bool FARString::Pos(size_t &nPos, wchar_t Ch, size_t nStartPos) const
{
const wchar_t *lpwszData = m_pContent->GetData();
const wchar_t *lpFound = wcschr(lpwszData + nStartPos, Ch);
if (lpFound)
{
nPos = lpFound - lpwszData;
return true;
}
return false;
}
bool FARString::Pos(size_t &nPos, const wchar_t *lpwszFind, size_t nStartPos) const
{
const wchar_t *lpwszData = m_pContent->GetData();
const wchar_t *lpFound = wcsstr(lpwszData + nStartPos, lpwszFind);
if (lpFound)
{
nPos = lpFound - lpwszData;
return true;
}
return false;
}
bool FARString::PosI(size_t &nPos, const wchar_t *lpwszFind, size_t nStartPos) const
{
const wchar_t *lpwszData = m_pContent->GetData();
const wchar_t *lpFound = StrStrI(lpwszData + nStartPos, lpwszFind);
if (lpFound)
{
nPos = lpFound - lpwszData;
return true;
}
return false;
}
bool FARString::RPos(size_t &nPos, wchar_t Ch, size_t nStartPos) const
{
const wchar_t *lpwszData = m_pContent->GetData();
const wchar_t *lpwszStrStart = lpwszData + nStartPos;
const wchar_t *lpwszStrScan = lpwszData + m_pContent->GetLength();
for (;;--lpwszStrScan)
{
if (*lpwszStrScan == Ch)
{
nPos = lpwszStrScan - lpwszData;
return true;
}
if (lpwszStrScan == lpwszStrStart)
return false;
}
}
std::string FARString::GetMB() const
{
std::string out;
Wide2MB(CPtr(), GetLength(), out);
return out;
}
size_t FARString::CellsCount() const
{
return StrCellsCount(CPtr(), GetLength());
}
size_t FARString::TruncateByCells(size_t nCount)
{
size_t ng = CellsCount();
if (ng > nCount) {
size_t ng = nCount;
auto sz = StrSizeOfCells(CPtr(), GetLength(), ng, false);
Truncate(sz);
}
return ng;
}
|