summaryrefslogtreecommitdiff
path: root/NxWidgets/libnxwidgets/src/cnxstring.cxx
blob: 512b681341a61551f999d608967e6385701e1431 (plain) (blame)
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
/****************************************************************************
 * NxWidgets/libnxwidgets/src/cnxstring.cxx
 *
 *   Copyright (C) 2012 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * 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. Neither the name NuttX, NxWidgets, nor the names of its contributors
 *    me be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "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
 * COPYRIGHT OWNER OR CONTRIBUTORS 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.
 *
 ****************************************************************************
 *
 * Portions of this package derive from Woopsi (http://woopsi.org/) and
 * portions are original efforts.  It is difficult to determine at this
 * point what parts are original efforts and which parts derive from Woopsi.
 * However, in any event, the work of  Antony Dzeryn will be acknowledged
 * in most NxWidget files.  Thanks Antony!
 *
 *   Copyright (c) 2007-2011, Antony Dzeryn
 *   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 names "Woopsi", "Simian Zombie" 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 Antony Dzeryn ``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 Antony Dzeryn 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.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#include "cnxstring.hxx"
#include "cstringiterator.hxx"

/****************************************************************************
 * Pre-Processor Definitions
 ****************************************************************************/

/****************************************************************************
 * CNxString Method Implementations
 ****************************************************************************/

using namespace NXWidgets;

/**
 * Constructor to create an empty string object.
 */

CNxString::CNxString()
{
  m_text          = (FAR nxwidget_char_t *)NULL;
  m_stringLength  = 0;
  m_allocatedSize = 0;
  m_growAmount    = 16;
}

/**
 * Constructor to create a string from a C character array.
 *
 * @param text Pointer to a char array to use as the basis of the
 * string.
 */

CNxString::CNxString(FAR const char *text)
{
  m_text          = (FAR nxwidget_char_t *)NULL;
  m_stringLength  = 0;
  m_allocatedSize = 0;
  m_growAmount    = 16;

  setText(text);
}

/**
 * Constructor to create a string from a single character.
 * @param letter Single character to use as the basis of the string.
 */

CNxString::CNxString(const nxwidget_char_t text)
{
  m_text          = (FAR nxwidget_char_t *)NULL;
  m_stringLength  = 0;
  m_allocatedSize = 0;
  m_growAmount    = 16;

  setText(text);
}

CNxString::CNxString(const CNxString &string)
{
  m_text          = (FAR nxwidget_char_t *)NULL;
  m_stringLength  = 0;
  m_allocatedSize = 0;
  m_growAmount    = 16;

  setText(string);
}

/**
 * Creates and returns a new CCStringIterator object that will iterate
 * over this string.  The object must be manually deleted once it is
 * no longer needed.
 *
 * @return A new CCStringIterator object.
 */

CStringIterator *CNxString::newStringIterator() const
{
  return new CStringIterator(this);
}

/**
 * Copy the internal array to the supplied buffer.  The buffer must be
 * large enough to contain the full text in the string.  The
 * getAllocSize() method can be used to obtain the length of the string.
 * Unlike the CNxString class, the char array is null-terminated.
 * The buffer must be (getAllocSize() + 2) bytes long, in order to
 * accommodate the terminator.
 *
 * @param buffer Buffer to copy the internal char array to.
 */

void CNxString::copyToCharArray(FAR nxwidget_char_t *buffer) const
{
  unsigned int dataLength = sizeof(nxwidget_char_t) * m_stringLength;
  memcpy(buffer, m_text, dataLength);
  buffer[dataLength] = '\0';
}

/**
 * Set the text in the string.
 *
 * @param text CNxString containing the new data for this string.
 */

void CNxString::setText(const CNxString &text)
{
  // Ensure we've got enough memory available

  allocateMemory(text.getLength(), false);

  // Copy the text into the internal array

  memcpy(m_text, text.getCharArray(), sizeof(nxwidget_char_t) * text.getLength());

  // Save size (in characters) of the string

  m_stringLength = text.getLength();
}

/**
 * Set the 8-bit C-string text in the string.
 *
 * @param text Char array to use as the new data for this string.
 */

void CNxString::setText(FAR const char *text)
{
  int length = strlen(text);

  // Ensure we've got enough memory available

  allocateMemory(length, false);

  // Copy characters into m_text, converting from 8- to 16-bit characters
  // (if necessary) and cache the length

  for (int i = 0; i < length; i++)
    {
      m_text[i] = (nxwidget_char_t)text[i];
    }

  m_stringLength = length;
}

/**
 * Set the text in the string.
 *
 * @param text Char array to use as the new data for this string.
 */

void CNxString::setText(FAR const nxwidget_char_t *text, int nchars)
{
  // Ensure we've got enough memory available

  allocateMemory(nchars, false);

  // Copy characters into m_text and cache the length

  m_stringLength = nchars;
  memcpy(m_text, text, sizeof(nxwidget_char_t) * nchars);
}

/**
 * Set the text in the string.
 *
 * @param text Character to to use as the new data for this string.
 */

void CNxString::setText(const nxwidget_char_t letter)
{
  // Ensure we've got enough memory available

  allocateMemory(1, false);

  // Copy the valid character into m_text and cache the length

  *m_text         = letter;
   m_stringLength = 1;
}

/**
 * Append text to the end of the string.
 *
 * @param text String to append.
 */

void CNxString::append(const CNxString &text)
{
  // Ensure we've got enough memory available

  allocateMemory(m_stringLength + text.getLength(), true);

  // Append the new string to the end of the array

  FAR nxwidget_char_t       *dest = &m_text[m_stringLength];
  FAR const nxwidget_char_t *src  = text.getCharArray();

  for (unsigned int i = 0; i < text.getLength(); i++)
    {
      *dest++ = *src++;
    }

  // Update the size in characters and the size in bytes

  m_stringLength += text.getLength();
}

/**
 * Insert text at the specified character index.
 *
 * @param text The text to insert.
 * @param index The index at which to insert the text.
 */

void CNxString::insert(const CNxString &text, int index)
{
  // Early exit if the string is empty

  if (!hasData())
    {
      CNxString::setText(text);
      return;
    }

  // Early exit if we're just appending

  if (index >= m_stringLength)
    {
      CNxString::append(text);
      return;
    }

  // Get the total size of the string that we need

  int newLength = m_stringLength + text.getLength();
  int newSize   = newLength * sizeof(nxwidget_char_t);

  // Reallocate memory if the existing memory isn't large enough

  if (m_allocatedSize < newSize)
    {
      int allocLength = newLength + m_growAmount;

      // Allocate new string large enough to contain additional data

      FAR nxwidget_char_t *newText = new nxwidget_char_t[allocLength];

      // Copy the start of the existing text to the newly allocated string

      FAR nxwidget_char_t       *dest = newText;
      FAR const nxwidget_char_t *src  = m_text;

      for (int i = 0; i < index; i++)
        {
          *dest++ = *src++;
        }

      // Insert the additional text into the new string

      src = text.getCharArray();

      for (unsigned int i = 0; i < text.getLength(); i++)
        {
          *dest++ = *src++;
        }

      // Copy the end of the existing text the the newly allocated string

      src = &m_text[index];

      for (int i = index; i < m_stringLength; i++)
        {
          *dest++ = *src++;
        }

      m_allocatedSize = allocLength * sizeof(nxwidget_char_t);

      // Delete existing string

      delete[] m_text;

      // Swap pointers

      m_stringLength = newLength;
      m_text         = newText;
    }
  else
    {
      // Existing size is large enough, so make space in string for insert

      FAR nxwidget_char_t       *dest = &m_text[newLength - 1];
      FAR const nxwidget_char_t *src  = &m_text[m_stringLength - 1];
      for (int i = 0; i < m_stringLength - index; i++)
        {
          *dest-- = *src--;
        }

      // Insert the additional text into the new string

      dest = &m_text[index];
      src  = text.getCharArray();

      for (unsigned int i = 0; i < text.getLength(); i++)
        {
          *dest++ = *src++;
        }

      m_stringLength = newLength;
    }
}

/**
 * Remove all characters from the string from the start index onwards.
 *
 * @param startIndex Index to remove from.
 */

void CNxString::remove(const int startIndex)
{
  // Reject if requested operation makes no sense

  if (!hasData() || startIndex >= m_stringLength)
    {
      return;
    }

  // Removing characters from the end of the string is trivial - simply
  // decrease the length

  m_stringLength  = startIndex;
}

/**
 * Remove specified number of characters from the string from the
 * start index onwards.
 *
 * @param startIndex Index to remove from.
 * @param count Number of characters to remove.
 */

void CNxString::remove(const int startIndex, const int count)
{
  // Reject if requested operation makes no sense

  if (!hasData() || startIndex >= m_stringLength)
    {
      return;
    }

  // Don't remove in this way if the count includes the end of the string

  int endIndex = startIndex + count;
  if (endIndex > m_stringLength)
    {
      remove(startIndex);
      return;
    }

  // Copy characters from a point after the area to be deleted into the space created
  // by the deletion

  FAR nxwidget_char_t       *dest = &m_text[startIndex];
  FAR const nxwidget_char_t *src  = &m_text[endIndex];

  for (int i = m_stringLength - endIndex; i > 0; i--)
    {
      *dest++ = *src++;
    }

  // Decrease length

  m_stringLength -= count;

}

/**
 * Get the character at the specified index.  This function is useful
 * for finding the occasional character at an index, but for iterating
 * over strings it is exceptionally slow.  The newStringIterator()
 * method should be used to retrieve an iterator object that can iterate
 * over the string efficiently.
 *
 * @param index The index of the character to retrieve.
 * @return The character at the specified index.
 */

const nxwidget_char_t CNxString::getCharAt(int index) const
{
  return *getCharPointer(index);
}

/**
 * Returns the first index of the specified letter within the string.
 * Will return -1 if the letter is not found.
 *
 * @param letter Letter to find.
 * @return The index of the letter.
 */

const int CNxString::indexOf(nxwidget_char_t letter) const
{
  return indexOf(letter, 0, getLength());
}

/**
 * Returns the first index of the specified letter within the string.
 * Will return -1 if the letter is not found.  Scans through the string
 * from "startIndex" until it has examined all subsequent letters.
 *
 * @param letter Letter to find.
 * @param startIndex The index to start searching from.
 * @return The index of the letter.
 */

const int CNxString::indexOf(nxwidget_char_t letter, int startIndex) const
{
  return indexOf(letter, startIndex, getLength() - startIndex);
}

/**
 * Returns the first index of the specified letter within the string.
 * Will return -1 if the letter is not found.  Scans through the string
 * from "startIndex" until it has examined all letters within the
 * range "count".
 *
 * @param letter Letter to find.
 * @param startIndex The index to start searching from.
 * @param count The number of characters to examine.
 * @return The index of the letter.
 */

const int CNxString::indexOf(nxwidget_char_t letter, int startIndex, int count) const
{
  // Exit if no data available

  if (!hasData())
    {
      return -1;
    }

  int index = -1;
  int charsExamined = 0;

  CStringIterator *iterator = new CStringIterator(this);
  if (!iterator->moveTo(startIndex))
    {
      delete iterator;
      return -1;
    }

  do
    {
      if (iterator->getChar() == letter)
        {
          index = iterator->getIndex();
          break;
        }

      charsExamined++;
    }
  while (iterator->moveToNext() && (charsExamined < count));

  delete iterator;
  return index;
}

/**
 * Returns the last index of the specified letter within the string.
 * Will return -1 if the letter is not found.
 *
 * @param letter Letter to find.
 * @return The index of the letter.
 */

const int CNxString::lastIndexOf(nxwidget_char_t letter) const
{
  return lastIndexOf(letter, getLength() - 1, getLength());
}

/**
 * Returns the last index of the specified letter within the string.
 * Will return -1 if the letter is not found.  Scans through the string
 * backwards from "startIndex" until it has examined all preceding
 * letters within the string.
 *
 * @param letter Letter to find.
 * @param startIndex The index to start searching from.
 * @return The index of the letter.
 */

const int CNxString::lastIndexOf(nxwidget_char_t letter, int startIndex) const
{
  return lastIndexOf(letter, startIndex, getLength() - (getLength() - startIndex));
}

/**
 * Returns the last index of the specified letter within the string.
 * Will return -1 if the letter is not found.  Scans through the string
 * backwards from "startIndex" until it has examined all letters within
 * the range "count".
 * @param letter Letter to find.
 * @param startIndex The index to start searching from.
 * @param count The number of characters to examine.
 * @return The index of the letter.
 */

const int CNxString::lastIndexOf(nxwidget_char_t letter, int startIndex, int count) const
{
  // Exit if no data available

  if (!hasData())
    {
      return -1;
    }

  int index = -1;
  int charsExamined = 0;

  CStringIterator *iterator = new CStringIterator(this);
  if (!iterator->moveTo(startIndex))
    {
      delete iterator;
      return -1;
    }

  do
    {
      if (iterator->getChar() == letter)
        {
          index = iterator->getIndex();
          break;
        }

      charsExamined++;
    }
  while (iterator->moveToPrevious() && (charsExamined <= count));

  delete iterator;
  return index;
}

/**
 * Get a substring from this string.  It is the responsibility of the
 * caller to delete the substring when it is no longer required.
 *
 * @param startIndex The starting point of the substring.
 * @return A pointer to a new CNxString object containing the
 * substring.
 */

CNxString* CNxString::subString(int startIndex) const
{
  return subString(startIndex, getLength() - startIndex);
}

/**
 * Get a substring from this string.  It is the responsibility of the
 * caller to delete the substring when it is no longer required.
 *
 * @param startIndex The starting point of the substring.
 * @param length The length of the substring.
 * @return A pointer to a new CNxString object containing the
 * substring.
*/

CNxString *CNxString::subString(int startIndex, int length) const
{
  CNxString *newString = new CNxString();
  CStringIterator *iterator = new CStringIterator(this);

  if (!iterator->moveTo(startIndex))
    {
      delete iterator;
      return (CNxString *)0;
    }

  // Build up the string character by character.  We could do a memcpy
  // here and improve performance.

  int count = 0;
  while (count < length)
    {
      newString->append(iterator->getChar());
      iterator->moveToNext();
      count++;
    }

  delete iterator;
  return newString;
}

/**
 * Overloaded assignment operator.  Copies the data within the argument
 * string to this string.
 *
 * @param string The string to copy.
 * @return This string.
 */

CNxString& CNxString::operator=(const CNxString &string)
{
  if (&string != this)
    {
      setText(string);
    }

  return *this;
}

/**
 * Overloaded assignment operator.  Copies the data within the argument
 * char array to this string.
 *
 * @param string The string to copy.
 * @return This string.
 */

CNxString& CNxString::operator=(FAR const char *string)
{
  setText(string);
  return *this;
}

/**
 * Overloaded assignment operator.  Copies the data from the argument
 * char to this string.
 *
 * @param letter The char to copy.
 * @return This string.
 */

CNxString& CNxString::operator=(const nxwidget_char_t letter)
{
  setText(letter);
  return *this;
}

/**
 * Compares this string to the argument.
 *
 * @param string String to compare to.
 * @return Zero if both strings are equal.  A value greater than zero
 * indicates that this string is greater than the argument string.  A
 * value less than zero indicates the opposite.  Note that the return
 * value indicates the *byte* that does not match, not the *character*.
 */

int CNxString::compareTo(const CNxString &string) const
{
  return memcmp((FAR const char*)m_text,
                (FAR const char*)string.getCharArray(),
                getLength());
}

/**
 * Allocate memory for the string.
 *
 * @param chars Number nxwidget_char_t size characters to allocate.
 * @param preserve If true, the data in the existing memory will be
 * preserved if new memory must be allocated
 */

void CNxString::allocateMemory(int nChars, bool preserve)
{
  // This is the size of the allocation that we need

  int nBytesNeeded = nChars * sizeof(nxwidget_char_t);

  // Do we already have enough memory allocated to contain this new size?
  // If so, we can avoid deallocating and allocating new memory by re-using the old

  if (nBytesNeeded > m_allocatedSize)
    {
      // Not enough space in existing memory; allocate new memory

      int allocChars = nChars + m_growAmount;
      nxwidget_char_t *newText = new nxwidget_char_t[allocChars];

      // Free old memory if necessary

      if (m_text != NULL)
        {
          // Preserve existing data if required

          if (preserve)
            {
              memcpy(newText, m_text, sizeof(nxwidget_char_t) * m_stringLength);
            }

          delete[] m_text;
        }

      // Set pointer to new memory

      m_text = newText;

      // Remember how much memory we've allocated.

      m_allocatedSize = allocChars * sizeof(nxwidget_char_t);
    }
}

/**
 * Return a pointer to the specified characters.
 *
 * @param index Index of the character to retrieve.
 */

FAR nxwidget_char_t *CNxString::getCharPointer(const int index) const
{
  // Early exit if the string is empty

  if (!hasData())
    {
      return (FAR nxwidget_char_t*)NULL;
    }

  // Early exit if the index is greater than the length of the string

  if (index >= m_stringLength)
    {
      return (FAR nxwidget_char_t*)NULL;
    }

  return &m_text[index];
}