1
2// vim:sw=2:ai
3
4/*
5 * Copyright (C) 2010-2011 DeNA Co.,Ltd.. All rights reserved.
6 * Copyright (C) 2011 Kentoku SHIBA
7 * See COPYRIGHT.txt for details.
8 */
9
10#ifndef DENA_STRING_REF_HPP
11#define DENA_STRING_REF_HPP
12
13namespace dena {
14
15struct string_wref {
16 typedef char value_type;
17 char *begin() const { return start; }
18 char *end() const { return start + length; }
19 size_t size() const { return length; }
20 private:
21 char *start;
22 size_t length;
23 public:
24 string_wref(char *s = 0, size_t len = 0) : start(s), length(len) { }
25};
26
27struct string_ref {
28 typedef const char value_type;
29 const char *begin() const { return start; }
30 const char *end() const { return start + length; }
31 size_t size() const { return length; }
32 void set(const char *s, size_t len) { start = s; length = len; }
33 void set(const char *s, const char *f) { start = s; length = f - s; }
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
60struct string_ref_list_wrap {
61 string_ref_list_wrap() {
62 if (SPD_INIT_DYNAMIC_ARRAY2(&string_ref_list, sizeof(string_ref),
63 NULL, 16, 16, MYF(MY_WME)))
64 string_ref_list_init = FALSE;
65 else
66 string_ref_list_init = TRUE;
67 }
68 virtual ~string_ref_list_wrap() {
69 if (string_ref_list_init) delete_dynamic(&string_ref_list); }
70 void clear() {
71 if (string_ref_list_init) string_ref_list.elements = 0; }
72 void push_back(string_ref &e) {
73 if (string_ref_list_init) insert_dynamic(&string_ref_list, (uchar*) &e);
74 return; }
75 size_t size() {
76 return string_ref_list_init ? string_ref_list.elements : 0; }
77 bool resize(size_t new_size) {
78 if (string_ref_list_init) {
79 if (string_ref_list.max_element < new_size && allocate_dynamic(
80 &string_ref_list, new_size)) return TRUE;
81 string_ref_list.elements = new_size;
82 return FALSE;
83 }
84 return TRUE;
85 }
86 bool empty() {
87 return string_ref_list_init ? string_ref_list.elements ?
88 FALSE : TRUE : TRUE; }
89 string_ref &operator [](size_t n) {
90 return ((string_ref *) (string_ref_list.buffer +
91 string_ref_list.size_of_element * n))[0]; }
92 bool string_ref_list_init;
93 DYNAMIC_ARRAY string_ref_list;
94};
95
96inline String *
97q_append_str(String *str, const char *p) {
98 uint32 p_len = strlen(p);
99 if (str->reserve(p_len)) return NULL;
100 str->q_append(p, p_len); return str;
101}
102
103};
104
105#endif
106
107