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
15namespace dena {
16
17struct 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
29struct 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
43template <size_t N> inline bool
44operator ==(const string_ref& x, const char (& y)[N]) {
45 return (x.size() == N - 1) && (::memcmp(x.begin(), y, N - 1) == 0);
46}
47
48inline bool
49operator ==(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
54inline bool
55operator !=(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