summaryrefslogtreecommitdiff
path: root/storage/mroonga/vendor/groonga/lib/ts/ts_str.c
blob: 6a8792eebc2473c180e24d1e3fd06b739b511d49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* -*- c-basic-offset: 2 -*- */
/*
  Copyright(C) 2015 Brazil

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License version 2.1 as published by the Free Software Foundation.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "ts_str.h"

#include <ctype.h>
#include <string.h>

/*-------------------------------------------------------------
 * Byte.
 */

grn_ts_bool
grn_ts_byte_is_decimal(uint8_t byte)
{
  return (byte >= '0') && (byte <= '9');
}

grn_ts_bool
grn_ts_byte_is_name_char(uint8_t byte)
{
  /*
   * Note: A table name allows '#', '@' and '-'.
   * http://groonga.org/docs/reference/commands/table_create.html#name
   */
  if (((byte >= '0') && (byte <= '9')) || ((byte >= 'A') && (byte <= 'Z')) ||
      ((byte >= 'a') && (byte <= 'z')) || (byte == '_')) {
    return GRN_TRUE;
  }
  return GRN_FALSE;
}

/*-------------------------------------------------------------
 * String.
 */

grn_ts_bool
grn_ts_str_starts_with(grn_ts_str str, grn_ts_str prefix)
{
  if (str.size < prefix.size) {
    return GRN_FALSE;
  }
  return !memcmp(str.ptr, prefix.ptr, prefix.size);
}

grn_ts_str
grn_ts_str_trim_left(grn_ts_str str)
{
  size_t i;
  for (i = 0; i < str.size; i++) {
    if (!isspace((uint8_t)str.ptr[i])) {
      break;
    }
  }
  str.ptr += i;
  str.size -= i;
  return str;
}

grn_ts_str
grn_ts_str_trim_score_assignment(grn_ts_str str)
{
  grn_ts_str rest;
  str = grn_ts_str_trim_left(str);
  if (!grn_ts_str_starts_with(str, (grn_ts_str){ "_score", 6 })) {
    return str;
  }
  rest.ptr = str.ptr + 6;
  rest.size = str.size - 6;
  rest = grn_ts_str_trim_left(rest);
  if (!rest.size || (rest.ptr[0] != '=') ||
      ((rest.size >= 2) && (rest.ptr[1] == '='))) {
    return str;
  }
  rest.ptr++;
  rest.size--;
  return grn_ts_str_trim_left(rest);
}

grn_ts_bool
grn_ts_str_has_number_prefix(grn_ts_str str)
{
  if (!str.size) {
    return GRN_FALSE;
  }
  if (grn_ts_byte_is_decimal(str.ptr[0])) {
    return GRN_TRUE;
  }
  if (str.size == 1) {
    return GRN_FALSE;
  }
  switch (str.ptr[0]) {
    case '+': case '-': {
      if (grn_ts_byte_is_decimal(str.ptr[1])) {
        return GRN_TRUE;
      }
      if (str.size == 2) {
        return GRN_FALSE;
      }
      return (str.ptr[1] == '.') && grn_ts_byte_is_decimal(str.ptr[2]);
    }
    case '.': {
      return grn_ts_byte_is_decimal(str.ptr[1]);
    }
    default: {
      return GRN_FALSE;
    }
  }
}

grn_ts_bool
grn_ts_str_is_name_prefix(grn_ts_str str)
{
  size_t i;
  for (i = 0; i < str.size; i++) {
    if (!grn_ts_byte_is_name_char(str.ptr[i])) {
      return GRN_FALSE;
    }
  }
  return GRN_TRUE;
}

grn_ts_bool
grn_ts_str_is_name(grn_ts_str str)
{
  if (!str.size) {
    return GRN_FALSE;
  }
  return grn_ts_str_is_name_prefix(str);
}

grn_ts_bool
grn_ts_str_is_true(grn_ts_str str)
{
  return (str.size == 4) && !memcmp(str.ptr, "true", 4);
}

grn_ts_bool
grn_ts_str_is_false(grn_ts_str str)
{
  return (str.size == 5) && !memcmp(str.ptr, "false", 5);
}

grn_ts_bool
grn_ts_str_is_bool(grn_ts_str str)
{
  return grn_ts_str_is_true(str) || grn_ts_str_is_false(str);
}

grn_ts_bool
grn_ts_str_is_id_name(grn_ts_str str)
{
  return (str.size == GRN_COLUMN_NAME_ID_LEN) &&
         !memcmp(str.ptr, GRN_COLUMN_NAME_ID, GRN_COLUMN_NAME_ID_LEN);
}

grn_ts_bool
grn_ts_str_is_score_name(grn_ts_str str)
{
  return (str.size == GRN_COLUMN_NAME_SCORE_LEN) &&
         !memcmp(str.ptr, GRN_COLUMN_NAME_SCORE, GRN_COLUMN_NAME_SCORE_LEN);
}

grn_ts_bool
grn_ts_str_is_key_name(grn_ts_str str)
{
  return (str.size == GRN_COLUMN_NAME_KEY_LEN) &&
         !memcmp(str.ptr, GRN_COLUMN_NAME_KEY, GRN_COLUMN_NAME_KEY_LEN);
}

grn_ts_bool
grn_ts_str_is_value_name(grn_ts_str str)
{
  return (str.size == GRN_COLUMN_NAME_VALUE_LEN) &&
         !memcmp(str.ptr, GRN_COLUMN_NAME_VALUE, GRN_COLUMN_NAME_VALUE_LEN);
}