include/am-string.h
author David Anderson <dvander@alliedmods.net>
Mon Aug 26 20:41:58 2013 -0700 (2013-08-26)
changeset 10 0ac292cc1467
parent 8 73549b7f37f5
child 12 cebfc7eeafa5
permissions -rw-r--r--
Add move semantics and fix some bugs.
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@10
    36
#include <am-moveable.h>
dvander@3
    37
dvander@3
    38
namespace ke {
dvander@3
    39
dvander@3
    40
// ASCII string.
dvander@3
    41
class AString
dvander@3
    42
{
dvander@3
    43
 public:
dvander@3
    44
  AString()
dvander@3
    45
   : length_(0)
dvander@3
    46
  {
dvander@3
    47
  }
dvander@3
    48
  explicit AString(const char *str) {
dvander@3
    49
    set(str, strlen(str));
dvander@3
    50
  }
dvander@3
    51
  AString(const char *str, size_t length) {
dvander@3
    52
    set(str, length);
dvander@3
    53
  }
dvander@3
    54
  AString(const AString &other) {
dvander@8
    55
    if (other.length_)
dvander@8
    56
      set(other.chars_, other.length_);
dvander@3
    57
  }
dvander@10
    58
  AString(Moveable<AString> other)
dvander@10
    59
    : chars_(other->chars_.take()),
dvander@10
    60
      length_(other->length_)
dvander@10
    61
  {
dvander@10
    62
    other->length_ = 0;
dvander@10
    63
  }
dvander@3
    64
dvander@3
    65
  AString &operator =(const char *str) {
dvander@8
    66
    if (str && str[0]) {
dvander@8
    67
      set(str, strlen(str));
dvander@8
    68
    } else {
dvander@8
    69
      chars_ = NULL;
dvander@8
    70
      length_ = 0;
dvander@8
    71
    }
dvander@3
    72
    return *this;
dvander@3
    73
  }
dvander@3
    74
  AString &operator =(const AString &other) {
dvander@8
    75
    if (other.length_) {
dvander@8
    76
      set(other.chars_, other.length_);
dvander@8
    77
    } else {
dvander@8
    78
      chars_ = NULL;
dvander@8
    79
      length_ = 0;
dvander@8
    80
    }
dvander@3
    81
    return *this;
dvander@3
    82
  }
dvander@3
    83
dvander@8
    84
  int compare(const AString &other) const {
dvander@8
    85
    return strcmp(chars(), other.chars());
dvander@8
    86
  }
dvander@3
    87
  bool operator ==(const AString &other) const {
dvander@3
    88
    return other.length() == length() &&
dvander@3
    89
           memcmp(other.chars(), chars(), length()) == 0;
dvander@3
    90
  }
dvander@3
    91
dvander@3
    92
  char operator [](size_t index) const {
dvander@3
    93
    assert(index < length());
dvander@3
    94
    return chars()[index];
dvander@3
    95
  }
dvander@3
    96
dvander@3
    97
  size_t length() const {
dvander@3
    98
    return length_;
dvander@3
    99
  }
dvander@3
   100
dvander@3
   101
  const char *chars() const {
dvander@3
   102
    if (!chars_)
dvander@3
   103
      return "";
dvander@3
   104
    return chars_;
dvander@3
   105
  }
dvander@3
   106
dvander@3
   107
 private:
dvander@3
   108
  void set(const char *str, size_t length) {
dvander@3
   109
    chars_ = new char[length + 1];
dvander@3
   110
    length_ = length;
dvander@3
   111
    memcpy(chars_, str, length);
dvander@3
   112
    chars_[length] = '\0';
dvander@3
   113
  }
dvander@3
   114
dvander@3
   115
 private:
dvander@3
   116
  AutoArray<char> chars_;
dvander@3
   117
  size_t length_;
dvander@3
   118
};
dvander@3
   119
dvander@3
   120
}
dvander@3
   121
dvander@3
   122
#endif // _include_amtl_string_h_