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