utils.h 1.7 KB
Newer Older
W
sdk-cpp  
wangguibao 已提交
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
/***************************************************************************
 * 
 * Copyright (c) 2018 Baidu.com, Inc. All Rights Reserved
 * 
 **************************************************************************/
 
 
 
/**
 * @file utils.h
 * @author root(com@baidu.com)
 * @date 2018/07/09 19:43:36
 * @brief 
 *  
 **/

#ifndef  BAIDU_PADDLE_SERVING_SDK_CPP_UTILS_H
#define  BAIDU_PADDLE_SERVING_SDK_CPP_UTILS_H

#include "common.h"

namespace baidu {
namespace paddle_serving {
namespace sdk_cpp {

inline int str_split(
        const std::string& source,
        const std::string& delim,
        std::vector<std::string>* vector_spliter) {

    int delim_length = delim.length();
    int total_length = source.length();
    int last = 0;

    if (delim_length == 0) {
        vector_spliter->push_back(source);
        return 0;
    }

    if (delim_length == 1) {
        size_t index = source.find_first_of(delim, last);
        while (index != std::string::npos) {
            vector_spliter->push_back(source.substr(last, 
                        index - last));
            last = index + delim_length;
            index = source.find_first_of(delim, last);
        }
    } else {
        size_t index = source.find(delim, last);
        while (index != std::string::npos) {
            vector_spliter->push_back(source.substr(last, 
                        index - last));
            last = index + delim_length;
            index = source.find(delim, last);
        }
    }

    if (last < total_length) {
        vector_spliter->push_back(source.substr(last, 
                    total_length - last));
    }
    return 0;
}

} // sdk_cpp
} // paddle_serving
} // baidu

#endif  //BAIDU_PADDLE_SERVING_SDK_CPP_UTILS_H

/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */