utils.h 1.8 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed 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.

#pragma once
#include <string>
#include <vector>
#include "sdk-cpp/include/common.h"
W
sdk-cpp  
wangguibao 已提交
19 20 21 22 23

namespace baidu {
namespace paddle_serving {
namespace sdk_cpp {

W
wangguibao 已提交
24 25 26 27 28 29
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;
W
sdk-cpp  
wangguibao 已提交
30

W
wangguibao 已提交
31 32 33 34 35 36 37 38 39 40 41
  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);
W
sdk-cpp  
wangguibao 已提交
42
    }
W
wangguibao 已提交
43 44 45 46 47 48
  } 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);
W
sdk-cpp  
wangguibao 已提交
49
    }
W
wangguibao 已提交
50
  }
W
sdk-cpp  
wangguibao 已提交
51

W
wangguibao 已提交
52 53 54 55
  if (last < total_length) {
    vector_spliter->push_back(source.substr(last, total_length - last));
  }
  return 0;
W
sdk-cpp  
wangguibao 已提交
56 57
}

W
wangguibao 已提交
58 59 60
}  // namespace sdk_cpp
}  // namespace paddle_serving
}  // namespace baidu