2 * Eddie Kohler, Yandong Mao, Robert Morris
3 * Copyright (c) 2012-2013 President and Fellows of Harvard College
4 * Copyright (c) 2012-2013 Massachusetts Institute of Technology
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, subject to the conditions
9 * listed in the Masstree LICENSE file. These conditions include: you must
10 * preserve this copyright notice, and you cannot mention the copyright
11 * holders in advertising related to the Software without their permission.
12 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
13 * notice is a summary of the Masstree LICENSE file; the license in that file
18 #include "string_base.hh"
23 struct Str : public String_base<Str> {
24 typedef Str substring_type;
25 typedef Str argument_type;
34 Str(const String_base<T>& x)
35 : s(x.data()), len(x.length()) {
38 : s(s_), len(strlen(s_)) {
40 Str(const char* s_, int len_)
43 Str(const unsigned char* s_, int len_)
44 : s(reinterpret_cast<const char*>(s_)), len(len_) {
46 Str(const char *first, const char *last)
47 : s(first), len(last - first) {
48 precondition(first <= last);
50 Str(const unsigned char *first, const unsigned char *last)
51 : s(reinterpret_cast<const char*>(first)), len(last - first) {
52 precondition(first <= last);
54 Str(const std::string& str)
55 : s(str.data()), len(str.length()) {
57 Str(const uninitialized_type &unused) {
61 static const Str maxkey;
68 void assign(const String_base<T> &x) {
72 void assign(const char *s_) {
76 void assign(const char *s_, int len_) {
81 const char *data() const {
87 char* mutable_data() {
88 return const_cast<char*>(s);
91 Str prefix(int lenx) const {
92 return Str(s, lenx < len ? lenx : len);
94 Str substring(const char *first, const char *last) const {
95 if (first <= last && first >= s && last <= s + len)
96 return Str(first, last);
100 Str substring(const unsigned char *first, const unsigned char *last) const {
101 const unsigned char *u = reinterpret_cast<const unsigned char*>(s);
102 if (first <= last && first >= u && last <= u + len)
103 return Str(first, last);
107 Str fast_substring(const char *first, const char *last) const {
108 assert(begin() <= first && first <= last && last <= end());
109 return Str(first, last);
111 Str fast_substring(const unsigned char *first, const unsigned char *last) const {
112 assert(ubegin() <= first && first <= last && last <= uend());
113 return Str(first, last);
116 return String_generic::ltrim(*this);
119 return String_generic::rtrim(*this);
122 return String_generic::trim(*this);
125 long to_i() const { // XXX does not handle negative
128 for (p = 0; p < len && s[p] >= '0' && s[p] <= '9'; ++p)
129 x = (x * 10) + s[p] - '0';
130 return p == len && p != 0 ? x : -1;
133 static Str snprintf(char *buf, size_t size, const char *fmt, ...) {
136 int n = vsnprintf(buf, size, fmt, val);
142 struct inline_string : public String_base<inline_string> {
146 const char *data() const {
153 size_t size() const {
154 return sizeof(inline_string) + len;
156 static size_t size(int len) {
157 return sizeof(inline_string) + len;
163 LCDF_MAKE_STRING_HASH(lcdf::Str)
164 LCDF_MAKE_STRING_HASH(lcdf::inline_string)