string_splitter_inl.h 8.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
gejun's avatar
gejun committed
17 18 19 20 21 22

// Author: Ge,Jun (gejun@baidu.com)
// Date: Mon. Apr. 18 19:52:34 CST 2011

#include <limits.h>

gejun's avatar
gejun committed
23 24
#ifndef BUTIL_STRING_SPLITTER_INL_H
#define BUTIL_STRING_SPLITTER_INL_H
gejun's avatar
gejun committed
25

26
namespace butil {
gejun's avatar
gejun committed
27 28 29 30 31 32 33 34 35 36 37 38

StringSplitter::StringSplitter(const char* str_begin,
                               const char* str_end,
                               const char sep,
                               EmptyFieldAction action)
    : _head(str_begin)
    , _str_tail(str_end)
    , _sep(sep)
    , _empty_field_action(action) {
    init();
}

39 40 41 42 43 44 45 46
StringSplitter::StringSplitter(const char* str, char sep,
                               EmptyFieldAction action)
    : StringSplitter(str, NULL, sep, action) {}

StringSplitter::StringSplitter(const StringPiece& input, char sep,
                               EmptyFieldAction action)
    : StringSplitter(input.data(), input.data() + input.length(), sep, action) {}

gejun's avatar
gejun committed
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
void StringSplitter::init() {
    // Find the starting _head and _tail.
    if (__builtin_expect(_head != NULL, 1)) {
        if (_empty_field_action == SKIP_EMPTY_FIELD) {
            for (; _sep == *_head && not_end(_head); ++_head) {}
        }
        for (_tail = _head; *_tail != _sep && not_end(_tail); ++_tail) {}
    } else {
        _tail = NULL;
    }
}

StringSplitter& StringSplitter::operator++() {
    if (__builtin_expect(_tail != NULL, 1)) {
        if (not_end(_tail)) {
            ++_tail;
            if (_empty_field_action == SKIP_EMPTY_FIELD) {
                for (; _sep == *_tail && not_end(_tail); ++_tail) {}
            }
        }
        _head = _tail;
        for (; *_tail != _sep && not_end(_tail); ++_tail) {}
    }
    return *this;
}

StringSplitter StringSplitter::operator++(int) {
    StringSplitter tmp = *this;
    operator++();
    return tmp;
}

StringSplitter::operator const void*() const {
    return (_head != NULL && not_end(_head)) ? _head : NULL;
}

const char* StringSplitter::field() const {
    return _head;
}

size_t StringSplitter::length() const {
    return static_cast<size_t>(_tail - _head);
}

91 92 93 94
StringPiece StringSplitter::field_sp() const {
    return StringPiece(field(), length());
}

gejun's avatar
gejun committed
95
bool StringSplitter::not_end(const char* p) const {
96
    return (_str_tail == NULL) ? *p : (p != _str_tail);
gejun's avatar
gejun committed
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
}

int StringSplitter::to_int8(int8_t* pv) const {
    long v = 0;
    if (to_long(&v) == 0 && v >= -128 && v <= 127) {
        *pv = (int8_t)v;
        return 0;
    }
    return -1;
}

int StringSplitter::to_uint8(uint8_t* pv) const {
    unsigned long v = 0;
    if (to_ulong(&v) == 0 && v <= 255) {
        *pv = (uint8_t)v;
        return 0;
    }
    return -1;
}

int StringSplitter::to_int(int* pv) const {
    long v = 0;
    if (to_long(&v) == 0 && v >= INT_MIN && v <= INT_MAX) {
        *pv = (int)v;
        return 0;
    }
    return -1;
}

int StringSplitter::to_uint(unsigned int* pv) const {
    unsigned long v = 0;
    if (to_ulong(&v) == 0 && v <= UINT_MAX) {
        *pv = (unsigned int)v;
        return 0;
    }
    return -1;
}

int StringSplitter::to_long(long* pv) const {
    char* endptr = NULL;
    *pv = strtol(field(), &endptr, 10);
    return (endptr == field() + length()) ? 0 : -1;
}

int StringSplitter::to_ulong(unsigned long* pv) const {
    char* endptr = NULL;
    *pv = strtoul(field(), &endptr, 10);
    return (endptr == field() + length()) ? 0 : -1;
}

int StringSplitter::to_longlong(long long* pv) const {
    char* endptr = NULL;
    *pv = strtoll(field(), &endptr, 10);
    return (endptr == field() + length()) ? 0 : -1;
}

int StringSplitter::to_ulonglong(unsigned long long* pv) const {
    char* endptr = NULL;
    *pv = strtoull(field(), &endptr, 10);
    return (endptr == field() + length()) ? 0 : -1;
}

int StringSplitter::to_float(float* pv) const {
    char* endptr = NULL;
    *pv = strtof(field(), &endptr);
    return (endptr == field() + length()) ? 0 : -1;
}

int StringSplitter::to_double(double* pv) const {
    char* endptr = NULL;
    *pv = strtod(field(), &endptr);
    return (endptr == field() + length()) ? 0 : -1;
}

StringMultiSplitter::StringMultiSplitter (
    const char* str, const char* seps, EmptyFieldAction action)
    : _head(str)
    , _str_tail(NULL)
    , _seps(seps)
    , _empty_field_action(action) {
    init();
}

StringMultiSplitter::StringMultiSplitter (
    const char* str_begin, const char* str_end,
    const char* seps, EmptyFieldAction action)
    : _head(str_begin)
    , _str_tail(str_end)
    , _seps(seps)
    , _empty_field_action(action) {
    init();
}

void StringMultiSplitter::init() {
    if (__builtin_expect(_head != NULL, 1)) {
        if (_empty_field_action == SKIP_EMPTY_FIELD) {
            for (; is_sep(*_head) && not_end(_head); ++_head) {}
        }
        for (_tail = _head; !is_sep(*_tail) && not_end(_tail); ++_tail) {}
    } else {
        _tail = NULL;
    }
}

StringMultiSplitter& StringMultiSplitter::operator++() {
    if (__builtin_expect(_tail != NULL, 1)) {
        if (not_end(_tail)) {
            ++_tail;
            if (_empty_field_action == SKIP_EMPTY_FIELD) {
                for (; is_sep(*_tail) && not_end(_tail); ++_tail) {}
            }
        }
        _head = _tail;
        for (; !is_sep(*_tail) && not_end(_tail); ++_tail) {}
    }
    return *this;
}

StringMultiSplitter StringMultiSplitter::operator++(int) {
    StringMultiSplitter tmp = *this;
    operator++();
    return tmp;
}

bool StringMultiSplitter::is_sep(char c) const {
222
    for (const char* p = _seps; *p != '\0'; ++p) {
gejun's avatar
gejun committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        if (c == *p) {
            return true;
        }
    }
    return false;
}

StringMultiSplitter::operator const void*() const {
    return (_head != NULL && not_end(_head)) ? _head : NULL;
}

const char* StringMultiSplitter::field() const {
    return _head;
}

size_t StringMultiSplitter::length() const {
    return static_cast<size_t>(_tail - _head);
}

242 243 244 245
StringPiece StringMultiSplitter::field_sp() const {
    return StringPiece(field(), length());
}

gejun's avatar
gejun committed
246
bool StringMultiSplitter::not_end(const char* p) const {
247
    return (_str_tail == NULL) ? *p : (p != _str_tail);
gejun's avatar
gejun committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
}

int StringMultiSplitter::to_int8(int8_t* pv) const {
    long v = 0;
    if (to_long(&v) == 0 && v >= -128 && v <= 127) {
        *pv = (int8_t)v;
        return 0;
    }
    return -1;
}

int StringMultiSplitter::to_uint8(uint8_t* pv) const {
    unsigned long v = 0;
    if (to_ulong(&v) == 0 && v <= 255) {
        *pv = (uint8_t)v;
        return 0;
    }
    return -1;
}

int StringMultiSplitter::to_int(int* pv) const {
    long v = 0;
    if (to_long(&v) == 0 && v >= INT_MIN && v <= INT_MAX) {
        *pv = (int)v;
        return 0;
    }
    return -1;
}

int StringMultiSplitter::to_uint(unsigned int* pv) const {
    unsigned long v = 0;
    if (to_ulong(&v) == 0 && v <= UINT_MAX) {
        *pv = (unsigned int)v;
        return 0;
    }
    return -1;
}

int StringMultiSplitter::to_long(long* pv) const {
    char* endptr = NULL;
    *pv = strtol(field(), &endptr, 10);
    return (endptr == field() + length()) ? 0 : -1;
}

int StringMultiSplitter::to_ulong(unsigned long* pv) const {
    char* endptr = NULL;
    *pv = strtoul(field(), &endptr, 10);
    return (endptr == field() + length()) ? 0 : -1;
}

int StringMultiSplitter::to_longlong(long long* pv) const {
    char* endptr = NULL;
    *pv = strtoll(field(), &endptr, 10);
    return (endptr == field() + length()) ? 0 : -1;
}

int StringMultiSplitter::to_ulonglong(unsigned long long* pv) const {
    char* endptr = NULL;
    *pv = strtoull(field(), &endptr, 10);
    return (endptr == field() + length()) ? 0 : -1;
}

int StringMultiSplitter::to_float(float* pv) const {
    char* endptr = NULL;
    *pv = strtof(field(), &endptr);
    return (endptr == field() + length()) ? 0 : -1;
}

int StringMultiSplitter::to_double(double* pv) const {
    char* endptr = NULL;
    *pv = strtod(field(), &endptr);
    return (endptr == field() + length()) ? 0 : -1;
}

322
void KeyValuePairsSplitter::UpdateDelimiterPosition() {
323
    const StringPiece key_value_pair(key_and_value());
324 325 326
    _delim_pos = key_value_pair.find(_key_value_delim);
    if (_delim_pos == StringPiece::npos) {
        _delim_pos = key_value_pair.length();
327 328 329
    }
}

330
}  // namespace butil
gejun's avatar
gejun committed
331

gejun's avatar
gejun committed
332
#endif  // BUTIL_STRING_SPLITTER_INL_H