include/am-string.h
author David Anderson <danderson@mozilla.com>
Tue Aug 27 00:49:33 2013 -0700 (2013-08-27)
changeset 12 cebfc7eeafa5
parent 10 0ac292cc1467
child 17 563aea5eba06
permissions -rw-r--r--
Added hash table tests.
     1 // vim: set sts=8 ts=2 sw=2 tw=99 et:
     2 //
     3 // Copyright (C) 2013, David Anderson and AlliedModders LLC
     4 // All rights reserved.
     5 // 
     6 // Redistribution and use in source and binary forms, with or without
     7 // modification, are permitted provided that the following conditions are met:
     8 // 
     9 //  * Redistributions of source code must retain the above copyright notice, this
    10 //    list of conditions and the following disclaimer.
    11 //  * Redistributions in binary form must reproduce the above copyright notice,
    12 //    this list of conditions and the following disclaimer in the documentation
    13 //    and/or other materials provided with the distribution.
    14 //  * Neither the name of AlliedModders LLC nor the names of its contributors
    15 //    may be used to endorse or promote products derived from this software
    16 //    without specific prior written permission.
    17 //
    18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    28 // POSSIBILITY OF SUCH DAMAGE.
    29 
    30 #ifndef _include_amtl_string_h_
    31 #define _include_amtl_string_h_
    32 
    33 #include <stdlib.h>
    34 #include <string.h>
    35 #include <am-utility.h>
    36 #include <am-moveable.h>
    37 
    38 namespace ke {
    39 
    40 // ASCII string.
    41 class AString
    42 {
    43  public:
    44   AString()
    45    : length_(0)
    46   {
    47   }
    48   explicit AString(const char *str) {
    49     set(str, strlen(str));
    50   }
    51   AString(const char *str, size_t length) {
    52     set(str, length);
    53   }
    54   AString(const AString &other) {
    55     if (other.length_)
    56       set(other.chars_, other.length_);
    57   }
    58   AString(Moveable<AString> other)
    59     : chars_(other->chars_.take()),
    60       length_(other->length_)
    61   {
    62     other->length_ = 0;
    63   }
    64 
    65   AString &operator =(const char *str) {
    66     if (str && str[0]) {
    67       set(str, strlen(str));
    68     } else {
    69       chars_ = NULL;
    70       length_ = 0;
    71     }
    72     return *this;
    73   }
    74   AString &operator =(const AString &other) {
    75     if (other.length_) {
    76       set(other.chars_, other.length_);
    77     } else {
    78       chars_ = NULL;
    79       length_ = 0;
    80     }
    81     return *this;
    82   }
    83 
    84   int compare(const char *str) const {
    85     return strcmp(chars(), str);
    86   }
    87   int compare(const AString &other) const {
    88     return strcmp(chars(), other.chars());
    89   }
    90   bool operator ==(const AString &other) const {
    91     return other.length() == length() &&
    92            memcmp(other.chars(), chars(), length()) == 0;
    93   }
    94 
    95   char operator [](size_t index) const {
    96     assert(index < length());
    97     return chars()[index];
    98   }
    99 
   100   size_t length() const {
   101     return length_;
   102   }
   103 
   104   const char *chars() const {
   105     if (!chars_)
   106       return "";
   107     return chars_;
   108   }
   109 
   110  private:
   111   void set(const char *str, size_t length) {
   112     chars_ = new char[length + 1];
   113     length_ = length;
   114     memcpy(chars_, str, length);
   115     chars_[length] = '\0';
   116   }
   117 
   118  private:
   119   AutoArray<char> chars_;
   120   size_t length_;
   121 };
   122 
   123 }
   124 
   125 #endif // _include_amtl_string_h_