1 | |
2 | // vim:sw=2:ai |
3 | |
4 | /* |
5 | * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. |
6 | * See COPYRIGHT.txt for details. |
7 | */ |
8 | |
9 | #ifndef DENA_STRING_REF_HPP |
10 | #define DENA_STRING_REF_HPP |
11 | |
12 | #include <vector> |
13 | #include <string.h> |
14 | |
15 | namespace dena { |
16 | |
17 | struct string_wref { |
18 | typedef char value_type; |
19 | char *begin() const { return start; } |
20 | char *end() const { return start + length; } |
21 | size_t size() const { return length; } |
22 | private: |
23 | char *start; |
24 | size_t length; |
25 | public: |
26 | string_wref(char *s = 0, size_t len = 0) : start(s), length(len) { } |
27 | }; |
28 | |
29 | struct string_ref { |
30 | typedef const char value_type; |
31 | const char *begin() const { return start; } |
32 | const char *end() const { return start + length; } |
33 | size_t size() const { return length; } |
34 | private: |
35 | const char *start; |
36 | size_t length; |
37 | public: |
38 | string_ref(const char *s = 0, size_t len = 0) : start(s), length(len) { } |
39 | string_ref(const char *s, const char *f) : start(s), length(f - s) { } |
40 | string_ref(const string_wref& w) : start(w.begin()), length(w.size()) { } |
41 | }; |
42 | |
43 | template <size_t N> inline bool |
44 | operator ==(const string_ref& x, const char (& y)[N]) { |
45 | return (x.size() == N - 1) && (::memcmp(x.begin(), y, N - 1) == 0); |
46 | } |
47 | |
48 | inline bool |
49 | operator ==(const string_ref& x, const string_ref& y) { |
50 | return (x.size() == y.size()) && |
51 | (::memcmp(x.begin(), y.begin(), x.size()) == 0); |
52 | } |
53 | |
54 | inline bool |
55 | operator !=(const string_ref& x, const string_ref& y) { |
56 | return (x.size() != y.size()) || |
57 | (::memcmp(x.begin(), y.begin(), x.size()) != 0); |
58 | } |
59 | |
60 | }; |
61 | |
62 | #endif |
63 | |
64 | |