forked from cpputest/cpputest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleString.cpp
More file actions
873 lines (721 loc) · 21.2 KB
/
Copy pathSimpleString.cpp
File metadata and controls
873 lines (721 loc) · 21.2 KB
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``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 <copyright holder> 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 "CppUTest/TestHarness.h"
#include "CppUTest/SimpleString.h"
#include "CppUTest/PlatformSpecificFunctions.h"
#include "CppUTest/TestMemoryAllocator.h"
TestMemoryAllocator* SimpleString::stringAllocator_ = NULLPTR;
TestMemoryAllocator* SimpleString::getStringAllocator()
{
if (stringAllocator_ == NULLPTR)
return defaultNewArrayAllocator();
return stringAllocator_;
}
void SimpleString::setStringAllocator(TestMemoryAllocator* allocator)
{
stringAllocator_ = allocator;
}
/* Avoid using the memory leak detector INSIDE SimpleString as its used inside the detector */
char* SimpleString::allocStringBuffer(size_t _size, const char* file, int line)
{
return getStringAllocator()->alloc_memory(_size, file, line);
}
void SimpleString::deallocStringBuffer(char* str, const char* file, int line)
{
getStringAllocator()->free_memory(str, file, line);
}
char* SimpleString::getEmptyString() const
{
char* empty = allocStringBuffer(1, __FILE__, __LINE__);
empty[0] = '\0';
return empty;
}
// does not support + or - prefixes
unsigned SimpleString::AtoU(const char* str)
{
while (isSpace(*str)) str++;
unsigned result = 0;
for(; isDigit(*str) && *str >= '0'; str++)
{
result *= 10;
result += static_cast<unsigned>(*str - '0');
}
return result;
}
int SimpleString::AtoI(const char* str)
{
while (isSpace(*str)) str++;
char first_char = *str;
if (first_char == '-' || first_char == '+') str++;
int result = 0;
for(; isDigit(*str); str++)
{
result *= 10;
result += *str - '0';
}
return (first_char == '-') ? -result : result;
}
int SimpleString::StrCmp(const char* s1, const char* s2)
{
while(*s1 && *s1 == *s2) {
++s1;
++s2;
}
return *(const unsigned char *) s1 - *(const unsigned char *) s2;
}
size_t SimpleString::StrLen(const char* str)
{
size_t n = (size_t)-1;
do n++; while (*str++);
return n;
}
int SimpleString::StrNCmp(const char* s1, const char* s2, size_t n)
{
while (n && *s1 && *s1 == *s2) {
--n;
++s1;
++s2;
}
return n ? *(const unsigned char *) s1 - *(const unsigned char *) s2 : 0;
}
char* SimpleString::StrNCpy(char* s1, const char* s2, size_t n)
{
char* result = s1;
if((NULLPTR == s1) || (0 == n)) return result;
while ((*s1++ = *s2++) && --n != 0)
;
return result;
}
const char* SimpleString::StrStr(const char* s1, const char* s2)
{
if(!*s2) return s1;
for (; *s1; s1++)
if (StrNCmp(s1, s2, StrLen(s2)) == 0)
return s1;
return NULLPTR;
}
char SimpleString::ToLower(char ch)
{
return isUpper(ch) ? (char)((int)ch + ('a' - 'A')) : ch;
}
int SimpleString::MemCmp(const void* s1, const void *s2, size_t n)
{
const unsigned char* p1 = (const unsigned char*) s1;
const unsigned char* p2 = (const unsigned char*) s2;
while (n--)
if (*p1 != *p2) {
return *p1 - *p2;
} else {
++p1;
++p2;
}
return 0;
}
SimpleString::SimpleString(const char *otherBuffer)
{
if (otherBuffer == NULLPTR) {
buffer_ = getEmptyString();
}
else {
buffer_ = copyToNewBuffer(otherBuffer);
}
}
SimpleString::SimpleString(const char *other, size_t repeatCount)
{
size_t otherStringLength = StrLen(other);
size_t len = otherStringLength * repeatCount + 1;
buffer_ = allocStringBuffer(len, __FILE__, __LINE__);
char* next = buffer_;
for (size_t i = 0; i < repeatCount; i++) {
StrNCpy(next, other, otherStringLength + 1);
next += otherStringLength;
}
*next = 0;
}
SimpleString::SimpleString(const SimpleString& other)
{
buffer_ = copyToNewBuffer(other.buffer_);
}
SimpleString& SimpleString::operator=(const SimpleString& other)
{
if (this != &other) {
deallocStringBuffer(buffer_, __FILE__, __LINE__);
buffer_ = copyToNewBuffer(other.buffer_);
}
return *this;
}
bool SimpleString::contains(const SimpleString& other) const
{
return StrStr(buffer_, other.buffer_) != NULLPTR;
}
bool SimpleString::containsNoCase(const SimpleString& other) const
{
return lowerCase().contains(other.lowerCase());
}
bool SimpleString::startsWith(const SimpleString& other) const
{
if (StrLen(other.buffer_) == 0) return true;
else if (size() == 0) return false;
else return StrStr(buffer_, other.buffer_) == buffer_;
}
bool SimpleString::endsWith(const SimpleString& other) const
{
size_t buffer_length = size();
size_t other_buffer_length = StrLen(other.buffer_);
if (other_buffer_length == 0) return true;
if (buffer_length == 0) return false;
if (buffer_length < other_buffer_length) return false;
return StrCmp(buffer_ + buffer_length - other_buffer_length, other.buffer_) == 0;
}
size_t SimpleString::count(const SimpleString& substr) const
{
size_t num = 0;
const char* str = buffer_;
while (*str && (str = StrStr(str, substr.buffer_))) {
num++;
str++;
}
return num;
}
void SimpleString::split(const SimpleString& delimiter, SimpleStringCollection& col) const
{
size_t num = count(delimiter);
size_t extraEndToken = (endsWith(delimiter)) ? 0 : 1U;
col.allocate(num + extraEndToken);
const char* str = buffer_;
const char* prev;
for (size_t i = 0; i < num; ++i) {
prev = str;
str = StrStr(str, delimiter.buffer_) + 1;
col[i] = SimpleString(prev).subString(0, size_t (str - prev));
}
if (extraEndToken) {
col[num] = str;
}
}
void SimpleString::replace(char to, char with)
{
size_t s = size();
for (size_t i = 0; i < s; i++) {
if (buffer_[i] == to) buffer_[i] = with;
}
}
void SimpleString::replace(const char* to, const char* with)
{
size_t c = count(to);
size_t len = size();
size_t tolen = StrLen(to);
size_t withlen = StrLen(with);
size_t newsize = len + (withlen * c) - (tolen * c) + 1;
if (newsize > 1) {
char* newbuf = allocStringBuffer(newsize, __FILE__, __LINE__);
for (size_t i = 0, j = 0; i < len;) {
if (StrNCmp(&buffer_[i], to, tolen) == 0) {
StrNCpy(&newbuf[j], with, withlen + 1);
j += withlen;
i += tolen;
}
else {
newbuf[j] = buffer_[i];
j++;
i++;
}
}
deallocStringBuffer(buffer_, __FILE__, __LINE__);
buffer_ = newbuf;
buffer_[newsize - 1] = '\0';
}
else {
deallocStringBuffer(buffer_, __FILE__, __LINE__);
buffer_ = getEmptyString();
}
}
SimpleString SimpleString::lowerCase() const
{
SimpleString str(*this);
size_t str_size = str.size();
for (size_t i = 0; i < str_size; i++)
str.buffer_[i] = ToLower(str.buffer_[i]);
return str;
}
const char *SimpleString::asCharString() const
{
return buffer_;
}
size_t SimpleString::size() const
{
return StrLen(buffer_);
}
bool SimpleString::isEmpty() const
{
return size() == 0;
}
SimpleString::~SimpleString()
{
deallocStringBuffer(buffer_, __FILE__, __LINE__);
}
bool operator==(const SimpleString& left, const SimpleString& right)
{
return 0 == SimpleString::StrCmp(left.asCharString(), right.asCharString());
}
bool SimpleString::equalsNoCase(const SimpleString& str) const
{
return lowerCase() == str.lowerCase();
}
bool operator!=(const SimpleString& left, const SimpleString& right)
{
return !(left == right);
}
SimpleString SimpleString::operator+(const SimpleString& rhs) const
{
SimpleString t(buffer_);
t += rhs.buffer_;
return t;
}
SimpleString& SimpleString::operator+=(const SimpleString& rhs)
{
return operator+=(rhs.buffer_);
}
SimpleString& SimpleString::operator+=(const char* rhs)
{
size_t originalSize = this->size();
size_t additionalStringSize = StrLen(rhs) + 1;
size_t sizeOfNewString = originalSize + additionalStringSize;
char* tbuffer = copyToNewBuffer(this->buffer_, sizeOfNewString);
StrNCpy(tbuffer + originalSize, rhs, additionalStringSize);
deallocStringBuffer(this->buffer_, __FILE__, __LINE__);
this->buffer_ = tbuffer;
return *this;
}
void SimpleString::padStringsToSameLength(SimpleString& str1, SimpleString& str2, char padCharacter)
{
if (str1.size() > str2.size()) {
padStringsToSameLength(str2, str1, padCharacter);
return;
}
char pad[2];
pad[0] = padCharacter;
pad[1] = 0;
str1 = SimpleString(pad, str2.size() - str1.size()) + str1;
}
SimpleString SimpleString::subString(size_t beginPos, size_t amount) const
{
if (beginPos > size()-1) return "";
SimpleString newString = buffer_ + beginPos;
if (newString.size() > amount)
newString.buffer_[amount] = '\0';
return newString;
}
SimpleString SimpleString::subString(size_t beginPos) const
{
return subString(beginPos, npos);
}
char SimpleString::at(size_t pos) const
{
return buffer_[pos];
}
size_t SimpleString::find(char ch) const
{
return findFrom(0, ch);
}
size_t SimpleString::findFrom(size_t starting_position, char ch) const
{
size_t length = size();
for (size_t i = starting_position; i < length; i++)
if (buffer_[i] == ch) return i;
return npos;
}
SimpleString SimpleString::subStringFromTill(char startChar, char lastExcludedChar) const
{
size_t beginPos = find(startChar);
if (beginPos == npos) return "";
size_t endPos = findFrom(beginPos, lastExcludedChar);
if (endPos == npos) return subString(beginPos);
return subString(beginPos, endPos - beginPos);
}
char* SimpleString::copyToNewBuffer(const char* bufferToCopy, size_t bufferSize)
{
if(bufferSize == 0) bufferSize = StrLen(bufferToCopy) + 1;
char* newBuffer = allocStringBuffer(bufferSize, __FILE__, __LINE__);
StrNCpy(newBuffer, bufferToCopy, bufferSize);
newBuffer[bufferSize-1] = '\0';
return newBuffer;
}
void SimpleString::copyToBuffer(char* bufferToCopy, size_t bufferSize) const
{
if (bufferToCopy == NULLPTR || bufferSize == 0) return;
size_t sizeToCopy = (bufferSize-1 < size()) ? (bufferSize-1) : size();
StrNCpy(bufferToCopy, buffer_, sizeToCopy);
bufferToCopy[sizeToCopy] = '\0';
}
bool SimpleString::isDigit(char ch)
{
return '0' <= ch && '9' >= ch;
}
bool SimpleString::isSpace(char ch)
{
return (ch == ' ') || (0x08 < ch && 0x0E > ch);
}
bool SimpleString::isUpper(char ch)
{
return 'A' <= ch && 'Z' >= ch;
}
SimpleString StringFrom(bool value)
{
return SimpleString(StringFromFormat("%s", value ? "true" : "false"));
}
SimpleString StringFrom(const char *value)
{
return SimpleString(value);
}
SimpleString StringFromOrNull(const char * expected)
{
return (expected) ? StringFrom(expected) : "(null)";
}
SimpleString StringFrom(int value)
{
return StringFromFormat("%d", value);
}
SimpleString StringFrom(long value)
{
return StringFromFormat("%ld", value);
}
SimpleString StringFrom(const void* value)
{
return SimpleString("0x") + HexStringFrom(value);
}
SimpleString StringFrom(void (*value)())
{
return SimpleString("0x") + HexStringFrom(value);
}
SimpleString HexStringFrom(long value)
{
return StringFromFormat("%lx", value);
}
SimpleString HexStringFrom(int value)
{
return StringFromFormat("%x", value);
}
SimpleString HexStringFrom(signed char value)
{
SimpleString result = StringFromFormat("%x", value);
if(value < 0) {
size_t size = result.size();
result = result.subString(size-(CPPUTEST_CHAR_BIT/4));
}
return result;
}
SimpleString HexStringFrom(unsigned long value)
{
return StringFromFormat("%lx", value);
}
SimpleString HexStringFrom(unsigned int value)
{
return StringFromFormat("%x", value);
}
SimpleString BracketsFormattedHexStringFrom(int value)
{
return BracketsFormattedHexString(HexStringFrom(value));
}
SimpleString BracketsFormattedHexStringFrom(unsigned int value)
{
return BracketsFormattedHexString(HexStringFrom(value));
}
SimpleString BracketsFormattedHexStringFrom(long value)
{
return BracketsFormattedHexString(HexStringFrom(value));
}
SimpleString BracketsFormattedHexStringFrom(unsigned long value)
{
return BracketsFormattedHexString(HexStringFrom(value));
}
SimpleString BracketsFormattedHexStringFrom(signed char value)
{
return BracketsFormattedHexString(HexStringFrom(value));
}
SimpleString BracketsFormattedHexString(SimpleString hexString)
{
return SimpleString("(0x") + hexString + ")" ;
}
/*
* ARM compiler has only partial support for C++11.
* Specifically std::nullptr_t is not officially supported
*/
#if __cplusplus > 199711L && !defined __arm__
SimpleString StringFrom(const std::nullptr_t __attribute__((unused)) value)
{
return "(null)";
}
#endif
#ifdef CPPUTEST_USE_LONG_LONG
SimpleString StringFrom(cpputest_longlong value)
{
return StringFromFormat("%lld", value);
}
SimpleString StringFrom(cpputest_ulonglong value)
{
return StringFromFormat("%llu", value);
}
SimpleString HexStringFrom(cpputest_longlong value)
{
return StringFromFormat("%llx", value);
}
SimpleString HexStringFrom(cpputest_ulonglong value)
{
return StringFromFormat("%llx", value);
}
SimpleString HexStringFrom(const void* value)
{
return HexStringFrom((cpputest_ulonglong) value);
}
SimpleString HexStringFrom(void (*value)())
{
return HexStringFrom((cpputest_ulonglong) value);
}
SimpleString BracketsFormattedHexStringFrom(cpputest_longlong value)
{
return BracketsFormattedHexString(HexStringFrom(value));
}
SimpleString BracketsFormattedHexStringFrom(cpputest_ulonglong value)
{
return BracketsFormattedHexString(HexStringFrom(value));
}
#else /* CPPUTEST_USE_LONG_LONG */
static long convertPointerToLongValue(const void* value)
{
/*
* This way of converting also can convert a 64bit pointer in a 32bit integer by truncating.
* This isn't the right way to convert pointers values and need to change by implementing a
* proper portable way to convert pointers to strings.
*/
long* long_value = (long*) &value;
return *long_value;
}
static long convertFunctionPointerToLongValue(void (*value)())
{
/*
* This way of converting also can convert a 64bit pointer in a 32bit integer by truncating.
* This isn't the right way to convert pointers values and need to change by implementing a
* proper portable way to convert pointers to strings.
*/
long* long_value = (long*) &value;
return *long_value;
}
SimpleString StringFrom(cpputest_longlong)
{
return "<longlong_unsupported>";
}
SimpleString StringFrom(cpputest_ulonglong)
{
return "<ulonglong_unsupported>";
}
SimpleString HexStringFrom(cpputest_longlong)
{
return "<longlong_unsupported>";
}
SimpleString HexStringFrom(cpputest_ulonglong)
{
return "<ulonglong_unsupported>";
}
SimpleString HexStringFrom(const void* value)
{
return StringFromFormat("%lx", convertPointerToLongValue(value));
}
SimpleString HexStringFrom(void (*value)())
{
return StringFromFormat("%lx", convertFunctionPointerToLongValue(value));
}
SimpleString BracketsFormattedHexStringFrom(cpputest_longlong)
{
return "";
}
SimpleString BracketsFormattedHexStringFrom(cpputest_ulonglong)
{
return "";
}
#endif /* CPPUTEST_USE_LONG_LONG */
SimpleString StringFrom(double value, int precision)
{
if (PlatformSpecificIsNan(value))
return "Nan - Not a number";
else if (PlatformSpecificIsInf(value))
return "Inf - Infinity";
else
return StringFromFormat("%.*g", precision, value);
}
SimpleString StringFrom(char value)
{
return StringFromFormat("%c", value);
}
SimpleString StringFrom(const SimpleString& value)
{
return SimpleString(value);
}
SimpleString StringFromFormat(const char* format, ...)
{
SimpleString resultString;
va_list arguments;
va_start(arguments, format);
resultString = VStringFromFormat(format, arguments);
va_end(arguments);
return resultString;
}
SimpleString StringFrom(unsigned int i)
{
return StringFromFormat("%u", i);
}
#if CPPUTEST_USE_STD_CPP_LIB
#include <string>
SimpleString StringFrom(const std::string& value)
{
return SimpleString(value.c_str());
}
#endif
SimpleString StringFrom(unsigned long i)
{
return StringFromFormat("%lu", i);
}
//Kludge to get a va_copy in VC++ V6
#ifndef va_copy
#define va_copy(copy, original) copy = original;
#endif
SimpleString VStringFromFormat(const char* format, va_list args)
{
va_list argsCopy;
va_copy(argsCopy, args);
enum
{
sizeOfdefaultBuffer = 100
};
char defaultBuffer[sizeOfdefaultBuffer];
SimpleString resultString;
size_t size = (size_t)PlatformSpecificVSNprintf(defaultBuffer, sizeOfdefaultBuffer, format, args);
if (size < sizeOfdefaultBuffer) {
resultString = SimpleString(defaultBuffer);
}
else {
size_t newBufferSize = size + 1;
char* newBuffer = SimpleString::allocStringBuffer(newBufferSize, __FILE__, __LINE__);
PlatformSpecificVSNprintf(newBuffer, newBufferSize, format, argsCopy);
resultString = SimpleString(newBuffer);
SimpleString::deallocStringBuffer(newBuffer, __FILE__, __LINE__);
}
va_end(argsCopy);
return resultString;
}
SimpleString StringFromBinary(const unsigned char* value, size_t size)
{
SimpleString result;
for (size_t i = 0; i < size; i++) {
result += StringFromFormat("%02X ", value[i]);
}
result = result.subString(0, result.size() - 1);
return result;
}
SimpleString StringFromBinaryOrNull(const unsigned char* value, size_t size)
{
return (value) ? StringFromBinary(value, size) : "(null)";
}
SimpleString StringFromBinaryWithSize(const unsigned char* value, size_t size)
{
SimpleString result = StringFromFormat("Size = %u | HexContents = ", (unsigned) size);
size_t displayedSize = ((size > 128) ? 128 : size);
result += StringFromBinaryOrNull(value, displayedSize);
if (size > displayedSize)
{
result += " ...";
}
return result;
}
SimpleString StringFromBinaryWithSizeOrNull(const unsigned char* value, size_t size)
{
return (value) ? StringFromBinaryWithSize(value, size) : "(null)";
}
SimpleString StringFromMaskedBits(unsigned long value, unsigned long mask, size_t byteCount)
{
SimpleString result;
size_t bitCount = (byteCount > sizeof(unsigned long)) ? (sizeof(unsigned long) * CPPUTEST_CHAR_BIT) : (byteCount * CPPUTEST_CHAR_BIT);
const unsigned long msbMask = (((unsigned long) 1) << (bitCount - 1));
for (size_t i = 0; i < bitCount; i++) {
if (mask & msbMask) {
result += (value & msbMask) ? "1" : "0";
}
else {
result += "x";
}
if (((i % 8) == 7) && (i != (bitCount - 1))) {
result += " ";
}
value <<= 1;
mask <<= 1;
}
return result;
}
SimpleString StringFromOrdinalNumber(unsigned int number)
{
unsigned int onesDigit = number % 10;
const char* suffix;
if (number >= 11 && number <= 13) {
suffix = "th";
} else if (3 == onesDigit) {
suffix = "rd";
} else if (2 == onesDigit) {
suffix = "nd";
} else if (1 == onesDigit) {
suffix = "st";
} else {
suffix = "th";
}
return StringFromFormat("%u%s", number, suffix);
}
SimpleStringCollection::SimpleStringCollection()
{
collection_ = NULLPTR;
size_ = 0;
}
void SimpleStringCollection::allocate(size_t _size)
{
delete[] collection_;
size_ = _size;
collection_ = new SimpleString[size_];
}
SimpleStringCollection::~SimpleStringCollection()
{
delete[] (collection_);
}
size_t SimpleStringCollection::size() const
{
return size_;
}
SimpleString& SimpleStringCollection::operator[](size_t index)
{
if (index >= size_) {
empty_ = "";
return empty_;
}
return collection_[index];
}