• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thetic / cpputest / 14350937103

09 Apr 2025 06:30AM UTC coverage: 96.186%. Remained the same
14350937103

push

github

thetic
no WorkingEnvironment

6254 of 6502 relevant lines covered (96.19%)

17609.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.25
/CppUTest/src/SimpleString.cpp
1
/*
2
 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *     * Redistributions of source code must retain the above copyright
8
 *       notice, this list of conditions and the following disclaimer.
9
 *     * Redistributions in binary form must reproduce the above copyright
10
 *       notice, this list of conditions and the following disclaimer in the
11
 *       documentation and/or other materials provided with the distribution.
12
 *     * Neither the name of the <organization> nor the
13
 *       names of its contributors may be used to endorse or promote products
14
 *       derived from this software without specific prior written permission.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ''AS IS'' AND ANY
17
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27

28
#include "CppUTest/TestHarness.h"
29
#include "CppUTest/SimpleString.h"
30
#include "CppUTest/PlatformSpecificFunctions.h"
31
#include "CppUTest/TestMemoryAllocator.h"
32

33
#include <limits.h>
34
#include <stdlib.h>
35

36
GlobalSimpleStringAllocatorStash::GlobalSimpleStringAllocatorStash()
130✔
37
    : originalAllocator_(nullptr)
130✔
38
{
39
}
130✔
40

41
void GlobalSimpleStringAllocatorStash::save()
130✔
42
{
43
    originalAllocator_ = SimpleString::getStringAllocator();
130✔
44
}
130✔
45

46
void GlobalSimpleStringAllocatorStash::restore()
130✔
47
{
48
    SimpleString::setStringAllocator(originalAllocator_);
130✔
49
}
130✔
50

51

52
GlobalSimpleStringMemoryAccountant::GlobalSimpleStringMemoryAccountant()
7✔
53
    : allocator_(nullptr)
7✔
54
{
55
    accountant_ = new MemoryAccountant();
7✔
56
}
7✔
57

58
GlobalSimpleStringMemoryAccountant::~GlobalSimpleStringMemoryAccountant()
7✔
59
{
60
    restoreAllocator();
7✔
61

62
    delete accountant_;
7✔
63
    delete allocator_;
7✔
64
}
7✔
65

66
void GlobalSimpleStringMemoryAccountant::restoreAllocator()
11✔
67
{
68
    if (allocator_ && (SimpleString::getStringAllocator() == allocator_))
11✔
69
        SimpleString::setStringAllocator(allocator_->originalAllocator());
4✔
70
}
11✔
71

72
void GlobalSimpleStringMemoryAccountant::useCacheSizes(size_t cacheSizes[], size_t length)
1✔
73
{
74
    accountant_->useCacheSizes(cacheSizes, length);
1✔
75
}
1✔
76

77
void GlobalSimpleStringMemoryAccountant::start()
7✔
78
{
79
    if (allocator_ != nullptr)
7✔
80
      return;
1✔
81

82
    allocator_ = new AccountingTestMemoryAllocator(*accountant_, SimpleString::getStringAllocator());
6✔
83

84
    SimpleString::setStringAllocator(allocator_);
6✔
85
}
86

87
void GlobalSimpleStringMemoryAccountant::stop()
6✔
88
{
89
    if (allocator_ == nullptr)
6✔
90
        FAIL("Global SimpleString allocator stopped without starting");
1✔
91

92
    if (SimpleString::getStringAllocator() != allocator_)
5✔
93
      FAIL("GlobalStrimpleStringMemoryAccountant: allocator has changed between start and stop!");
1✔
94

95
    restoreAllocator();
4✔
96
}
4✔
97

98
SimpleString GlobalSimpleStringMemoryAccountant::report()
2✔
99
{
100
    return accountant_->report();
2✔
101
}
102

103
AccountingTestMemoryAllocator* GlobalSimpleStringMemoryAccountant::getAllocator()
1✔
104
{
105
    return allocator_;
1✔
106
}
107

108
TestMemoryAllocator* SimpleString::stringAllocator_ = nullptr;
109

110
TestMemoryAllocator* SimpleString::getStringAllocator()
952,704✔
111
{
112
    if (stringAllocator_ == nullptr)
952,704✔
113
        return defaultNewArrayAllocator();
320✔
114
    return stringAllocator_;
952,384✔
115
}
116

117
void SimpleString::setStringAllocator(TestMemoryAllocator* allocator)
419✔
118
{
119
    stringAllocator_ = allocator;
419✔
120
}
419✔
121

122
/* Avoid using the memory leak detector INSIDE SimpleString as its used inside the detector */
123
char* SimpleString::allocStringBuffer(size_t _size, const char* file, size_t line)
476,385✔
124
{
125
    return getStringAllocator()->alloc_memory(_size, file, line);
476,385✔
126
}
127

128
void SimpleString::deallocStringBuffer(char* str, size_t size, const char* file, size_t line)
476,081✔
129
{
130
    getStringAllocator()->free_memory(str, size, file, line);
476,081✔
131
}
476,081✔
132

133
char* SimpleString::getEmptyString() const
68✔
134
{
135
    char* empty = allocStringBuffer(1, __FILE__, __LINE__);
68✔
136
    empty[0] = '\0';
68✔
137
    return empty;
68✔
138
}
139

140
// does not support + or - prefixes
141
unsigned SimpleString::AtoU(const char* str)
19✔
142
{
143
    while (isSpace(*str)) str++;
24✔
144

145
    unsigned result = 0;
19✔
146
    for(; isDigit(*str) && *str >= '0'; str++)
62✔
147
    {
148
        result *= 10;
43✔
149
        result += static_cast<unsigned>(*str - '0');
43✔
150
    }
151
    return result;
19✔
152
}
153

154
int SimpleString::AtoI(const char* str)
12✔
155
{
156
    while (isSpace(*str)) str++;
16✔
157

158
    char first_char = *str;
12✔
159
    if (first_char == '-' || first_char == '+') str++;
12✔
160

161
    int  result = 0;
12✔
162
    for(; isDigit(*str); str++)
55✔
163
    {
164
        result *= 10;
43✔
165
        result += *str - '0';
43✔
166
    }
167
    return (first_char == '-') ? -result : result;
12✔
168
}
169

170
int SimpleString::StrCmp(const char* s1, const char* s2)
299,046✔
171
{
172
   while(*s1 && *s1 == *s2) {
2,529,483✔
173
       ++s1;
2,230,437✔
174
       ++s2;
2,230,437✔
175
   }
176
   return *(const unsigned char *) s1 - *(const unsigned char *) s2;
299,046✔
177
}
178

179
size_t SimpleString::StrLen(const char* str)
1,184,823✔
180
{
181
    size_t n = (size_t)-1;
1,184,823✔
182
    do n++; while (*str++);
21,797,883✔
183
    return n;
1,184,823✔
184
}
185

186
int SimpleString::StrNCmp(const char* s1, const char* s2, size_t n)
676,112✔
187
{
188
    while (n && *s1 && *s1 == *s2) {
809,902✔
189
        --n;
133,790✔
190
        ++s1;
133,790✔
191
        ++s2;
133,790✔
192
    }
193
    return n ? *(const unsigned char *) s1 - *(const unsigned char *) s2 : 0;
676,112✔
194
}
195

196
char* SimpleString::StrNCpy(char* s1, const char* s2, size_t n)
516,012✔
197
{
198
    char* result = s1;
516,012✔
199

200
    if((nullptr == s1) || (0 == n)) return result;
516,012✔
201

202
    *s1 = *s2;
516,010✔
203
    while ((--n != 0) && *s1){
8,660,096✔
204
        *++s1 = *++s2;
8,144,086✔
205
    }
206
    return result;
516,010✔
207
}
208

209
const char* SimpleString::StrStr(const char* s1, const char* s2)
5,444✔
210
{
211
    if(!*s2) return s1;
5,444✔
212
    for (; *s1; s1++)
675,251✔
213
        if (StrNCmp(s1, s2, StrLen(s2)) == 0)
673,649✔
214
            return s1;
3,825✔
215
    return nullptr;
1,602✔
216
}
217

218
char SimpleString::ToLower(char ch)
195✔
219
{
220
    return isUpper(ch) ? (char)((int)ch + ('a' - 'A')) : ch;
195✔
221
}
222

223
int SimpleString::MemCmp(const void* s1, const void *s2, size_t n)
21✔
224
{
225
    const unsigned char* p1 = (const unsigned char*) s1;
21✔
226
    const unsigned char* p2 = (const unsigned char*) s2;
21✔
227

228
    while (n--)
78✔
229
        if (*p1 != *p2) {
67✔
230
            return *p1 - *p2;
10✔
231
        } else {
232
            ++p1;
57✔
233
            ++p2;
57✔
234
        }
235
    return 0;
11✔
236
}
237

238
void SimpleString::deallocateInternalBuffer()
898,706✔
239
{
240
    if (buffer_) {
898,706✔
241
        deallocStringBuffer(buffer_, bufferSize_, __FILE__, __LINE__);
475,994✔
242
        buffer_ = nullptr;
475,994✔
243
        bufferSize_ = 0;
475,994✔
244
    }
245
}
898,706✔
246

247
void SimpleString::setInternalBufferAsEmptyString()
68✔
248
{
249
    deallocateInternalBuffer();
68✔
250

251
    bufferSize_ = 1;
68✔
252
    buffer_ = getEmptyString();
68✔
253
}
68✔
254

255
void SimpleString::copyBufferToNewInternalBuffer(const char* otherBuffer, size_t bufferSize)
447,891✔
256
{
257
    deallocateInternalBuffer();
447,891✔
258

259
    bufferSize_ = bufferSize;
447,891✔
260
    buffer_ = copyToNewBuffer(otherBuffer, bufferSize_);
447,891✔
261
}
447,891✔
262

263
void SimpleString::setInternalBufferToNewBuffer(size_t bufferSize)
273✔
264
{
265
    deallocateInternalBuffer();
273✔
266

267
    bufferSize_ = bufferSize;
273✔
268
    buffer_ = allocStringBuffer(bufferSize_, __FILE__, __LINE__);
273✔
269
    buffer_[0] = '\0';
273✔
270
}
273✔
271

272
void SimpleString::setInternalBufferTo(char* buffer, size_t bufferSize)
28,066✔
273
{
274
    deallocateInternalBuffer();
28,066✔
275

276
    bufferSize_ = bufferSize;
28,066✔
277
    buffer_ = buffer;
28,066✔
278
}
28,066✔
279

280
void SimpleString::copyBufferToNewInternalBuffer(const SimpleString& otherBuffer)
25,408✔
281
{
282
    copyBufferToNewInternalBuffer(otherBuffer.buffer_, otherBuffer.size() + 1);
25,408✔
283
}
25,408✔
284

285
void SimpleString::copyBufferToNewInternalBuffer(const char* otherBuffer)
422,483✔
286
{
287
    copyBufferToNewInternalBuffer(otherBuffer, StrLen(otherBuffer) + 1);
422,483✔
288
}
422,483✔
289

290
const char* SimpleString::getBuffer() const
744,480✔
291
{
292
    return buffer_;
744,480✔
293
}
294

295
SimpleString::SimpleString(const char *otherBuffer)
409,334✔
296
    : buffer_(nullptr), bufferSize_(0)
409,334✔
297
{
298
    if (otherBuffer == nullptr)
409,334✔
299
        setInternalBufferAsEmptyString();
67✔
300
    else
301
        copyBufferToNewInternalBuffer(otherBuffer);
409,267✔
302
}
409,334✔
303

304
SimpleString::SimpleString(const char *other, size_t repeatCount)
162✔
305
    : buffer_(nullptr), bufferSize_(0)
162✔
306
{
307
    size_t otherStringLength = StrLen(other);
162✔
308
    setInternalBufferToNewBuffer(otherStringLength * repeatCount + 1);
162✔
309

310
    char* next = buffer_;
162✔
311
    for (size_t i = 0; i < repeatCount; i++) {
11,887✔
312
        StrNCpy(next, other, otherStringLength + 1);
11,725✔
313
        next += otherStringLength;
11,725✔
314
    }
315
    *next = 0;
162✔
316
}
162✔
317

318
SimpleString::SimpleString(const SimpleString& other)
13,216✔
319
    : buffer_(nullptr), bufferSize_(0)
13,216✔
320
{
321
    copyBufferToNewInternalBuffer(other.getBuffer());
13,216✔
322
}
13,216✔
323

324
SimpleString& SimpleString::operator=(const SimpleString& other)
25,408✔
325
{
326
    if (this != &other)
25,408✔
327
        copyBufferToNewInternalBuffer(other);
25,408✔
328
    return *this;
25,408✔
329
}
330

331
bool SimpleString::contains(const SimpleString& other) const
2,261✔
332
{
333
    return StrStr(getBuffer(), other.getBuffer()) != nullptr;
2,261✔
334
}
335

336
bool SimpleString::containsNoCase(const SimpleString& other) const
4✔
337
{
338
    return lowerCase().contains(other.lowerCase());
4✔
339
}
340

341
bool SimpleString::startsWith(const SimpleString& other) const
1,337✔
342
{
343
    if (other.size() == 0) return true;
1,337✔
344
    else if (size() == 0) return false;
1,337✔
345
    else return StrStr(getBuffer(), other.getBuffer()) == getBuffer();
1,337✔
346
}
347

348
bool SimpleString::endsWith(const SimpleString& other) const
83✔
349
{
350
    size_t length = size();
83✔
351
    size_t other_length = other.size();
83✔
352

353
    if (other_length == 0) return true;
83✔
354
    if (length == 0) return false;
83✔
355
    if (length < other_length) return false;
82✔
356

357
    return StrCmp(getBuffer() + length - other_length, other.getBuffer()) == 0;
81✔
358
}
359

360
size_t SimpleString::count(const SimpleString& substr) const
504✔
361
{
362
    size_t num = 0;
504✔
363
    const char* str = getBuffer();
504✔
364
    const char* strpart = nullptr;
504✔
365
    if (*str){
504✔
366
        strpart = StrStr(str, substr.getBuffer());
304✔
367
    }
368
    while (*str && strpart) {
1,389✔
369
        str = strpart;
885✔
370
        str++;
885✔
371
        num++;
885✔
372
        strpart = StrStr(str, substr.getBuffer());
885✔
373
    }
374
    return num;
504✔
375
}
376

377
void SimpleString::split(const SimpleString& delimiter, SimpleStringCollection& col) const
70✔
378
{
379
    size_t num = count(delimiter);
70✔
380
    size_t extraEndToken = (endsWith(delimiter)) ? 0 : 1U;
70✔
381
    col.allocate(num + extraEndToken);
70✔
382

383
    const char* str = getBuffer();
70✔
384
    const char* prev;
385
    for (size_t i = 0; i < num; ++i) {
722✔
386
        prev = str;
652✔
387
        str = StrStr(str, delimiter.getBuffer()) + 1;
652✔
388
        col[i] = SimpleString(prev).subString(0, size_t (str - prev));
652✔
389
    }
390
    if (extraEndToken) {
70✔
391
        col[num] = str;
9✔
392
    }
393
}
70✔
394

395
void SimpleString::replace(char to, char with)
361✔
396
{
397
    size_t s = size();
361✔
398
    for (size_t i = 0; i < s; i++) {
9,301✔
399
        if (getBuffer()[i] == to) buffer_[i] = with;
8,940✔
400
    }
401
}
361✔
402

403
void SimpleString::replace(const char* to, const char* with)
427✔
404
{
405
    size_t c = count(to);
427✔
406
    if (c == 0) {
427✔
407
        return;
385✔
408
    }
409
    size_t len = size();
42✔
410
    size_t tolen = StrLen(to);
42✔
411
    size_t withlen = StrLen(with);
42✔
412

413
    size_t newsize = len + (withlen * c) - (tolen * c) + 1;
42✔
414

415
    if (newsize > 1) {
42✔
416
        char* newbuf = allocStringBuffer(newsize, __FILE__, __LINE__);
41✔
417
        for (size_t i = 0, j = 0; i < len;) {
2,483✔
418
            if (StrNCmp(&getBuffer()[i], to, tolen) == 0) {
2,442✔
419
                StrNCpy(&newbuf[j], with, withlen + 1);
204✔
420
                j += withlen;
204✔
421
                i += tolen;
204✔
422
            }
423
            else {
424
                newbuf[j] = getBuffer()[i];
2,238✔
425
                j++;
2,238✔
426
                i++;
2,238✔
427
            }
428
        }
429
        newbuf[newsize - 1] = '\0';
41✔
430
        setInternalBufferTo(newbuf, newsize);
41✔
431
    }
432
    else
433
        setInternalBufferAsEmptyString();
1✔
434
}
435

436
SimpleString SimpleString::printable() const
111✔
437
{
438
    static const char* shortEscapeCodes[] =
439
    {
440
        "\\a",
441
        "\\b",
442
        "\\t",
443
        "\\n",
444
        "\\v",
445
        "\\f",
446
        "\\r"
447
    };
448

449
    SimpleString result;
111✔
450
    result.setInternalBufferToNewBuffer(getPrintableSize() + 1);
111✔
451

452
    size_t str_size = size();
111✔
453
    size_t j = 0;
111✔
454
    for (size_t i = 0; i < str_size; i++)
616✔
455
    {
456
        char c = buffer_[i];
505✔
457
        if (isControlWithShortEscapeSequence(c))
505✔
458
        {
459
            StrNCpy(&result.buffer_[j], shortEscapeCodes[(unsigned char)(c - '\a')], 2);
11✔
460
            j += 2;
11✔
461
        }
462
        else if (isControl(c))
494✔
463
        {
464
            SimpleString hexEscapeCode = StringFromFormat("\\x%02X ", c);
5✔
465
            StrNCpy(&result.buffer_[j], hexEscapeCode.asCharString(), 4);
5✔
466
            j += 4;
5✔
467
        }
5✔
468
        else
469
        {
470
            result.buffer_[j] = c;
489✔
471
            j++;
489✔
472
        }
473
    }
474
    result.buffer_[j] = 0;
111✔
475

476
    return result;
111✔
477
}
×
478

479
size_t SimpleString::getPrintableSize() const
111✔
480
{
481
    size_t str_size = size();
111✔
482
    size_t printable_str_size = str_size;
111✔
483

484
    for (size_t i = 0; i < str_size; i++)
616✔
485
    {
486
        char c = buffer_[i];
505✔
487
        if (isControlWithShortEscapeSequence(c))
505✔
488
        {
489
            printable_str_size += 1;
11✔
490
        }
491
        else if (isControl(c))
494✔
492
        {
493
            printable_str_size += 3;
5✔
494
        }
495
    }
496

497
    return printable_str_size;
111✔
498
}
499

500
SimpleString SimpleString::lowerCase() const
25✔
501
{
502
    SimpleString str(*this);
25✔
503

504
    size_t str_size = str.size();
25✔
505
    for (size_t i = 0; i < str_size; i++)
144✔
506
        str.buffer_[i] = ToLower(str.getBuffer()[i]);
119✔
507

508
    return str;
25✔
509
}
×
510

511
const char *SimpleString::asCharString() const
609,609✔
512
{
513
    return getBuffer();
609,609✔
514
}
515

516
size_t SimpleString::size() const
60,304✔
517
{
518
    return StrLen(getBuffer());
60,304✔
519
}
520

521
bool SimpleString::isEmpty() const
1,230✔
522
{
523
    return size() == 0;
1,230✔
524
}
525

526
SimpleString::~SimpleString()
422,408✔
527
{
528
    deallocateInternalBuffer();
422,408✔
529
}
422,408✔
530

531
bool operator==(const SimpleString& left, const SimpleString& right)
297,449✔
532
{
533
    return 0 == SimpleString::StrCmp(left.asCharString(), right.asCharString());
297,449✔
534
}
535

536
bool SimpleString::equalsNoCase(const SimpleString& str) const
8✔
537
{
538
    return lowerCase() == str.lowerCase();
8✔
539
}
540

541

542
bool operator!=(const SimpleString& left, const SimpleString& right)
57,724✔
543
{
544
    return !(left == right);
57,724✔
545
}
546

547
SimpleString SimpleString::operator+(const SimpleString& rhs) const
1,147✔
548
{
549
    SimpleString t(getBuffer());
1,147✔
550
    t += rhs.getBuffer();
1,147✔
551
    return t;
1,147✔
552
}
×
553

554
SimpleString& SimpleString::operator+=(const SimpleString& rhs)
4,886✔
555
{
556
    return operator+=(rhs.getBuffer());
4,886✔
557
}
558

559
SimpleString& SimpleString::operator+=(const char* rhs)
28,025✔
560
{
561
    size_t originalSize = this->size();
28,025✔
562
    size_t additionalStringSize = StrLen(rhs) + 1;
28,025✔
563
    size_t sizeOfNewString = originalSize + additionalStringSize;
28,025✔
564
    char* tbuffer = copyToNewBuffer(this->getBuffer(), sizeOfNewString);
28,025✔
565
    StrNCpy(tbuffer + originalSize, rhs, additionalStringSize);
28,025✔
566

567
    setInternalBufferTo(tbuffer, sizeOfNewString);
28,025✔
568
    return *this;
28,025✔
569
}
570

571
void SimpleString::padStringsToSameLength(SimpleString& str1, SimpleString& str2, char padCharacter)
36✔
572
{
573
    if (str1.size() > str2.size()) {
36✔
574
        padStringsToSameLength(str2, str1, padCharacter);
3✔
575
        return;
3✔
576
    }
577

578
    char pad[2];
579
    pad[0] = padCharacter;
33✔
580
    pad[1] = 0;
33✔
581
    str1 = SimpleString(pad, str2.size() - str1.size()) + str1;
33✔
582
}
583

584
SimpleString SimpleString::subString(size_t beginPos, size_t amount) const
792✔
585
{
586
    if (beginPos > size()-1) return "";
792✔
587

588
    SimpleString newString = getBuffer() + beginPos;
791✔
589

590
    if (newString.size() > amount)
791✔
591
        newString.buffer_[amount] = '\0';
709✔
592

593
    return newString;
791✔
594
}
791✔
595

596
SimpleString SimpleString::subString(size_t beginPos) const
11✔
597
{
598
    return subString(beginPos, npos);
11✔
599
}
600

601
char SimpleString::at(size_t pos) const
503✔
602
{
603
    return getBuffer()[pos];
503✔
604
}
605

606
size_t SimpleString::find(char ch) const
11✔
607
{
608
    return findFrom(0, ch);
11✔
609
}
610

611
size_t SimpleString::findFrom(size_t starting_position, char ch) const
18✔
612
{
613
    size_t length = size();
18✔
614
    for (size_t i = starting_position; i < length; i++)
124✔
615
        if (at(i) == ch) return i;
120✔
616
    return npos;
4✔
617
}
618

619
SimpleString SimpleString::subStringFromTill(char startChar, char lastExcludedChar) const
8✔
620
{
621
    size_t beginPos = find(startChar);
8✔
622
    if (beginPos == npos) return "";
8✔
623

624
    size_t endPos = findFrom(beginPos, lastExcludedChar);
7✔
625
    if (endPos == npos) return subString(beginPos);
7✔
626

627
    return subString(beginPos, endPos - beginPos);
5✔
628
}
629

630
char* SimpleString::copyToNewBuffer(const char* bufferToCopy, size_t bufferSize)
475,916✔
631
{
632
    char* newBuffer = allocStringBuffer(bufferSize, __FILE__, __LINE__);
475,916✔
633
    StrNCpy(newBuffer, bufferToCopy, bufferSize);
475,916✔
634
    newBuffer[bufferSize-1] = '\0';
475,916✔
635
    return newBuffer;
475,916✔
636
}
637

638

639
void SimpleString::copyToBuffer(char* bufferToCopy, size_t bufferSize) const
4✔
640
{
641
    if (bufferToCopy == nullptr || bufferSize == 0) return;
4✔
642

643
    size_t sizeToCopy = (bufferSize-1 < size()) ? (bufferSize-1) : size();
3✔
644

645
    StrNCpy(bufferToCopy, getBuffer(), sizeToCopy);
3✔
646
    bufferToCopy[sizeToCopy] = '\0';
3✔
647
}
648

649
bool SimpleString::isDigit(char ch)
117✔
650
{
651
    return '0' <= ch && '9' >= ch;
117✔
652
}
653

654
bool SimpleString::isSpace(char ch)
40✔
655
{
656
    return (ch == ' ') || (0x08 < ch && 0x0E > ch);
40✔
657
}
658

659
bool SimpleString::isUpper(char ch)
195✔
660
{
661
    return 'A' <= ch && 'Z' >= ch;
195✔
662
}
663

664
bool SimpleString::isControl(char ch)
988✔
665
{
666
    return ch < ' ' || ch == char(0x7F);
988✔
667
}
668

669
bool SimpleString::isControlWithShortEscapeSequence(char ch)
1,010✔
670
{
671
    return '\a' <= ch && '\r' >= ch;
1,010✔
672
}
673

674
SimpleString StringFrom(bool value)
4✔
675
{
676
    return SimpleString(StringFromFormat("%s", value ? "true" : "false"));
4✔
677
}
678

679
SimpleString StringFrom(const char *value)
146✔
680
{
681
    return SimpleString(value);
146✔
682
}
683

684
SimpleString StringFromOrNull(const char * expected)
9✔
685
{
686
    return (expected) ? StringFrom(expected) : StringFrom("(null)");
9✔
687
}
688

689
SimpleString PrintableStringFromOrNull(const char * expected)
121✔
690
{
691
    return (expected) ? StringFrom(expected).printable() : StringFrom("(null)");
242✔
692
}
693

694
SimpleString StringFrom(int value)
104✔
695
{
696
    return StringFromFormat("%d", value);
104✔
697
}
698

699
SimpleString StringFrom(long value)
39✔
700
{
701
    return StringFromFormat("%ld", value);
39✔
702
}
703

704
SimpleString StringFrom(const void* value)
20✔
705
{
706
    return SimpleString("0x") + HexStringFrom(value);
20✔
707
}
708

709
SimpleString StringFrom(void (*value)())
7✔
710
{
711
    return SimpleString("0x") + HexStringFrom(value);
7✔
712
}
713

714
SimpleString HexStringFrom(long value)
33✔
715
{
716
    return HexStringFrom((unsigned long)value);
33✔
717
}
718

719
SimpleString HexStringFrom(int value)
53✔
720
{
721
    return HexStringFrom((unsigned int)value);
53✔
722
}
723

724
SimpleString HexStringFrom(signed char value)
8✔
725
{
726
    SimpleString result = StringFromFormat("%x", value);
8✔
727
    if(value < 0) {
8✔
728
        size_t size = result.size();
6✔
729
        result = result.subString(size-(CHAR_BIT/4));
6✔
730
    }
731
    return result;
8✔
732
}
×
733

734
SimpleString HexStringFrom(unsigned long value)
54✔
735
{
736
    return StringFromFormat("%lx", value);
54✔
737
}
738

739
SimpleString HexStringFrom(unsigned int value)
70✔
740
{
741
    return StringFromFormat("%x", value);
70✔
742
}
743

744
SimpleString BracketsFormattedHexStringFrom(int value)
53✔
745
{
746
    return BracketsFormattedHexString(HexStringFrom(value));
53✔
747
}
748

749
SimpleString BracketsFormattedHexStringFrom(unsigned int value)
17✔
750
{
751
    return BracketsFormattedHexString(HexStringFrom(value));
17✔
752
}
753

754
SimpleString BracketsFormattedHexStringFrom(long value)
32✔
755
{
756
    return BracketsFormattedHexString(HexStringFrom(value));
32✔
757
}
758

759

760
SimpleString BracketsFormattedHexStringFrom(unsigned long value)
21✔
761
{
762
    return BracketsFormattedHexString(HexStringFrom(value));
21✔
763
}
764

765
SimpleString BracketsFormattedHexStringFrom(signed char value)
7✔
766
{
767
    return BracketsFormattedHexString(HexStringFrom(value));
7✔
768
}
769

770
SimpleString BracketsFormattedHexString(SimpleString hexString)
156✔
771
{
772
    return SimpleString("(0x") + hexString + ")" ;
312✔
773
}
774

775
/*
776
 * ARM compiler has only partial support for C++11.
777
 * Specifically nullptr_t is not officially supported
778
 */
779
#if __cplusplus > 199711L && !defined __arm__ && CPPUTEST_USE_STD_CPP_LIB
780
SimpleString StringFrom(const std::nullptr_t value)
1✔
781
{
782
    (void) value;
783
    return "(null)";
1✔
784
}
785
#endif
786

787
SimpleString StringFrom(long long value)
13✔
788
{
789
    return StringFromFormat("%lld", value);
13✔
790
}
791

792
SimpleString StringFrom(unsigned long long value)
14✔
793
{
794
    return StringFromFormat("%llu", value);
14✔
795
}
796

797
SimpleString HexStringFrom(long long value)
14✔
798
{
799
    return HexStringFrom((unsigned long long)value);
14✔
800
}
801

802
SimpleString HexStringFrom(unsigned long long value)
61✔
803
{
804
    return StringFromFormat("%llx", value);
61✔
805
}
806

807
SimpleString HexStringFrom(const void* value)
25✔
808
{
809
    return HexStringFrom((unsigned long long) value);
25✔
810
}
811

812
SimpleString HexStringFrom(void (*value)())
9✔
813
{
814
    return HexStringFrom((unsigned long long) value);
9✔
815
}
816

817
SimpleString BracketsFormattedHexStringFrom(long long value)
13✔
818
{
819
    return BracketsFormattedHexString(HexStringFrom(value));
13✔
820
}
821

822

823
SimpleString BracketsFormattedHexStringFrom(unsigned long long value)
13✔
824
{
825
    return BracketsFormattedHexString(HexStringFrom(value));
13✔
826
}
827

828
SimpleString StringFrom(double value, int precision)
58✔
829
{
830
    if (PlatformSpecificIsNan(value))
58✔
831
        return "Nan - Not a number";
5✔
832
    else if (PlatformSpecificIsInf(value))
53✔
833
        return "Inf - Infinity";
4✔
834
    else
835
        return StringFromFormat("%.*g", precision, value);
49✔
836
}
837

838
SimpleString StringFrom(char value)
9✔
839
{
840
    return StringFromFormat("%c", value);
9✔
841
}
842

843
SimpleString StringFrom(const SimpleString& value)
1✔
844
{
845
    return SimpleString(value);
1✔
846
}
847

848
SimpleString StringFromFormat(const char* format, ...)
10,617✔
849
{
850
    SimpleString resultString;
10,617✔
851
    va_list arguments;
852
    va_start(arguments, format);
10,617✔
853

854
    resultString = VStringFromFormat(format, arguments);
10,617✔
855
    va_end(arguments);
10,617✔
856
    return resultString;
21,234✔
857
}
×
858

859
SimpleString StringFrom(unsigned int i)
18✔
860
{
861
    return StringFromFormat("%u", i);
18✔
862
}
863

864
#if CPPUTEST_USE_STD_CPP_LIB
865

866
#include <string>
867

868
SimpleString StringFrom(const std::string& value)
1✔
869
{
870
    return SimpleString(value.c_str());
1✔
871
}
872

873
#endif
874

875
SimpleString StringFrom(unsigned long i)
3,595✔
876
{
877
    return StringFromFormat("%lu", i);
3,595✔
878
}
879

880
SimpleString VStringFromFormat(const char* format, va_list args)
10,617✔
881
{
882
    va_list argsCopy;
883
    va_copy(argsCopy, args);
10,617✔
884
    enum
885
    {
886
        sizeOfdefaultBuffer = 100
887
    };
888
    char defaultBuffer[sizeOfdefaultBuffer];
889
    SimpleString resultString;
10,617✔
890

891
    size_t size = (size_t)PlatformSpecificVSNprintf(defaultBuffer, sizeOfdefaultBuffer, format, args);
10,617✔
892
    if (size < sizeOfdefaultBuffer) {
10,617✔
893
        resultString = SimpleString(defaultBuffer);
10,530✔
894
    }
895
    else {
896
        size_t newBufferSize = size + 1;
87✔
897
        char* newBuffer = SimpleString::allocStringBuffer(newBufferSize, __FILE__, __LINE__);
87✔
898
        PlatformSpecificVSNprintf(newBuffer, newBufferSize, format, argsCopy);
87✔
899
        resultString = SimpleString(newBuffer);
87✔
900

901
        SimpleString::deallocStringBuffer(newBuffer, newBufferSize, __FILE__, __LINE__);
87✔
902
    }
903
    va_end(argsCopy);
10,617✔
904
    return resultString;
21,234✔
905
}
×
906

907
SimpleString StringFromBinary(const unsigned char* value, size_t size)
48✔
908
{
909
    SimpleString result;
48✔
910

911
    for (size_t i = 0; i < size; i++) {
340✔
912
        result += StringFromFormat("%02X ", value[i]);
292✔
913
    }
914
    result = result.subString(0, result.size() - 1);
48✔
915

916
    return result;
48✔
917
}
×
918

919
SimpleString StringFromBinaryOrNull(const unsigned char* value, size_t size)
51✔
920
{
921
    return (value) ? StringFromBinary(value, size) : StringFrom("(null)");
51✔
922
}
923

924
SimpleString StringFromBinaryWithSize(const unsigned char* value, size_t size)
21✔
925
{
926
    SimpleString result = StringFromFormat("Size = %u | HexContents = ", (unsigned) size);
21✔
927
    size_t displayedSize = ((size > 128) ? 128 : size);
21✔
928
    result += StringFromBinaryOrNull(value, displayedSize);
21✔
929
    if (size > displayedSize)
21✔
930
    {
931
        result += " ...";
1✔
932
    }
933
    return result;
21✔
934
}
×
935

936
SimpleString StringFromBinaryWithSizeOrNull(const unsigned char* value, size_t size)
19✔
937
{
938
    return (value) ? StringFromBinaryWithSize(value, size) : StringFrom("(null)");
19✔
939
}
940

941
SimpleString StringFromMaskedBits(unsigned long value, unsigned long mask, size_t byteCount)
34✔
942
{
943
    SimpleString result;
34✔
944
    size_t bitCount = (byteCount > sizeof(unsigned long)) ? (sizeof(unsigned long) * CHAR_BIT) : (byteCount * CHAR_BIT);
34✔
945
    const unsigned long msbMask = (((unsigned long) 1) << (bitCount - 1));
34✔
946

947
    for (size_t i = 0; i < bitCount; i++) {
706✔
948
        if (mask & msbMask) {
672✔
949
            result += (value & msbMask) ? "1" : "0";
370✔
950
        }
951
        else {
952
            result += "x";
302✔
953
        }
954

955
        if (((i % 8) == 7) && (i != (bitCount - 1))) {
672✔
956
            result += " ";
50✔
957
        }
958

959
        value <<= 1;
672✔
960
        mask <<= 1;
672✔
961
    }
962

963
    return result;
34✔
964
}
×
965

966
SimpleString StringFromOrdinalNumber(unsigned int number)
33✔
967
{
968
    const char* suffix = "th";
33✔
969

970
    if ((number < 11) || (number > 13)) {
33✔
971
        unsigned int const onesDigit = number % 10;
27✔
972
        if (3 == onesDigit) {
27✔
973
            suffix = "rd";
6✔
974
        } else if (2 == onesDigit) {
21✔
975
            suffix = "nd";
7✔
976
        } else if (1 == onesDigit) {
14✔
977
            suffix = "st";
3✔
978
        }
979
    }
980

981
    return StringFromFormat("%u%s", number, suffix);
33✔
982
}
983

984
SimpleStringCollection::SimpleStringCollection()
49✔
985
{
986
    collection_ = nullptr;
49✔
987
    size_ = 0;
49✔
988
}
49✔
989

990
void SimpleStringCollection::allocate(size_t _size)
74✔
991
{
992
    delete[] collection_;
385✔
993

994
    size_ = _size;
74✔
995
    collection_ = new SimpleString[size_];
751✔
996
}
74✔
997

998
SimpleStringCollection::~SimpleStringCollection()
49✔
999
{
1000
    delete[] (collection_);
415✔
1001
}
49✔
1002

1003
size_t SimpleStringCollection::size() const
15✔
1004
{
1005
    return size_;
15✔
1006
}
1007

1008
SimpleString& SimpleStringCollection::operator[](size_t index)
743✔
1009
{
1010
    if (index >= size_) {
743✔
1011
        empty_ = "";
3✔
1012
        return empty_;
3✔
1013
    }
1014

1015
    return collection_[index];
740✔
1016
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc