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