benchmark silo added
[c11concurrency-benchmarks.git] / silo / masstree / str.hh
1 /* Masstree
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
5  *
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
14  * is legally binding.
15  */
16 #ifndef STR_HH
17 #define STR_HH
18 #include "string_base.hh"
19 #include <stdarg.h>
20 #include <stdio.h>
21 namespace lcdf {
22
23 struct Str : public String_base<Str> {
24     typedef Str substring_type;
25     typedef Str argument_type;
26
27     const char *s;
28     int len;
29
30     Str()
31         : s(0), len(0) {
32     }
33     template <typename T>
34     Str(const String_base<T>& x)
35         : s(x.data()), len(x.length()) {
36     }
37     Str(const char* s_)
38         : s(s_), len(strlen(s_)) {
39     }
40     Str(const char* s_, int len_)
41         : s(s_), len(len_) {
42     }
43     Str(const unsigned char* s_, int len_)
44         : s(reinterpret_cast<const char*>(s_)), len(len_) {
45     }
46     Str(const char *first, const char *last)
47         : s(first), len(last - first) {
48         precondition(first <= last);
49     }
50     Str(const unsigned char *first, const unsigned char *last)
51         : s(reinterpret_cast<const char*>(first)), len(last - first) {
52         precondition(first <= last);
53     }
54     Str(const std::string& str)
55         : s(str.data()), len(str.length()) {
56     }
57     Str(const uninitialized_type &unused) {
58         (void) unused;
59     }
60
61     static const Str maxkey;
62
63     void assign() {
64         s = 0;
65         len = 0;
66     }
67     template <typename T>
68     void assign(const String_base<T> &x) {
69         s = x.data();
70         len = x.length();
71     }
72     void assign(const char *s_) {
73         s = s_;
74         len = strlen(s_);
75     }
76     void assign(const char *s_, int len_) {
77         s = s_;
78         len = len_;
79     }
80
81     const char *data() const {
82         return s;
83     }
84     int length() const {
85         return len;
86     }
87     char* mutable_data() {
88         return const_cast<char*>(s);
89     }
90
91     Str prefix(int lenx) const {
92         return Str(s, lenx < len ? lenx : len);
93     }
94     Str substring(const char *first, const char *last) const {
95         if (first <= last && first >= s && last <= s + len)
96             return Str(first, last);
97         else
98             return Str();
99     }
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);
104         else
105             return Str();
106     }
107     Str fast_substring(const char *first, const char *last) const {
108         assert(begin() <= first && first <= last && last <= end());
109         return Str(first, last);
110     }
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);
114     }
115     Str ltrim() const {
116         return String_generic::ltrim(*this);
117     }
118     Str rtrim() const {
119         return String_generic::rtrim(*this);
120     }
121     Str trim() const {
122         return String_generic::trim(*this);
123     }
124
125     long to_i() const {         // XXX does not handle negative
126         long x = 0;
127         int p;
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;
131     }
132
133     static Str snprintf(char *buf, size_t size, const char *fmt, ...) {
134         va_list val;
135         va_start(val, fmt);
136         int n = vsnprintf(buf, size, fmt, val);
137         va_end(val);
138         return Str(buf, n);
139     }
140 };
141
142 struct inline_string : public String_base<inline_string> {
143     int len;
144     char s[0];
145
146     const char *data() const {
147         return s;
148     }
149     int length() const {
150         return len;
151     }
152
153     size_t size() const {
154         return sizeof(inline_string) + len;
155     }
156     static size_t size(int len) {
157         return sizeof(inline_string) + len;
158     }
159 };
160
161 } // namespace lcdf
162
163 LCDF_MAKE_STRING_HASH(lcdf::Str)
164 LCDF_MAKE_STRING_HASH(lcdf::inline_string)
165 #endif