string.cpp 5.7 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
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
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2010
//

#include "common/lang/string.h"

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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>

#include "common/log/log.h"
namespace common {

31 32 33
char *strip(char *str_)
{
  if (str_ == NULL || *str_ == 0) {
羽飞's avatar
羽飞 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    LOG_ERROR("The augument is invalid!");
    return str_;
  }

  char *head = str_;
  while (isspace(*head))
    ++head;

  char *last = str_ + strlen(str_) - 1;
  while (isspace(*last) && last != str_)
    --last;
  *(last + 1) = 0;
  return head;
}

49 50
void strip(std::string &str)
{
羽飞's avatar
羽飞 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  size_t head = 0;

  while (isspace(str[head])) {
    ++head;
  }

  size_t tail = str.size() - 1;
  while (isspace(str[tail]) && tail != head) {
    --tail;
  }

  str = str.substr(head, (tail - head) + 1);
}

// Translation functions with templates are defined in the header file
66 67
std::string size_to_pad_str(int size, int pad)
{
羽飞's avatar
羽飞 已提交
68 69 70 71 72
  std::ostringstream ss;
  ss << std::setw(pad) << std::setfill('0') << size;
  return ss.str();
}

73 74
std::string &str_to_upper(std::string &s)
{
羽飞's avatar
羽飞 已提交
75 76 77 78
  std::transform(s.begin(), s.end(), s.begin(), (int (*)(int)) & std::toupper);
  return s;
}

79 80
std::string &str_to_lower(std::string &s)
{
羽飞's avatar
羽飞 已提交
81 82 83 84
  std::transform(s.begin(), s.end(), s.begin(), (int (*)(int)) & std::tolower);
  return s;
}

85 86
void split_string(const std::string &str, std::string delim, std::set<std::string> &results)
{
羽飞's avatar
羽飞 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100
  int cut_at;
  std::string tmp_str(str);
  while ((cut_at = tmp_str.find_first_of(delim)) != (signed)tmp_str.npos) {
    if (cut_at > 0) {
      results.insert(tmp_str.substr(0, cut_at));
    }
    tmp_str = tmp_str.substr(cut_at + 1);
  }

  if (tmp_str.length() > 0) {
    results.insert(tmp_str);
  }
}

101 102
void split_string(const std::string &str, std::string delim, std::vector<std::string> &results)
{
羽飞's avatar
羽飞 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116
  int cut_at;
  std::string tmp_str(str);
  while ((cut_at = tmp_str.find_first_of(delim)) != (signed)tmp_str.npos) {
    if (cut_at > 0) {
      results.push_back(tmp_str.substr(0, cut_at));
    }
    tmp_str = tmp_str.substr(cut_at + 1);
  }

  if (tmp_str.length() > 0) {
    results.push_back(tmp_str);
  }
}

117 118
void split_string(char *str, char dim, std::vector<char *> &results, bool keep_null)
{
羽飞's avatar
羽飞 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  char *p = str;
  char *l = p;
  while (*p) {
    if (*p == dim) {
      *p++ = 0;
      if (p - l > 1 || keep_null)
        results.push_back(l);
      l = p;
    } else
      ++p;
  }
  if (p - l > 0 || keep_null)
    results.push_back(l);
  return;
}

135 136
void merge_string(std::string &str, std::string delim, std::vector<std::string> &source, size_t result_len)
{
羽飞's avatar
羽飞 已提交
137 138

  std::ostringstream ss;
139
  if (source.empty()) {
羽飞's avatar
羽飞 已提交
140 141 142 143 144 145 146 147 148 149 150
    str = ss.str();
    return;
  }

  if (result_len == 0 || result_len > source.size()) {
    result_len = source.size();
  }

  for (unsigned int i = 0; i < result_len; i++) {
    if (i == 0) {
      ss << source[i];
151
    } else {
羽飞's avatar
羽飞 已提交
152 153 154 155 156
      ss << delim << source[i];
    }
  }

  str = ss.str();
157
  return;
羽飞's avatar
羽飞 已提交
158 159
}

160 161
void replace(std::string &str, const std::string &old, const std::string &new_str)
{
羽飞's avatar
羽飞 已提交
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
  if (old.compare(new_str) == 0) {
    return;
  }

  if (old == "") {
    return;
  }

  if (old.length() > str.length()) {
    return;
  }

  std::string result;

  size_t index;
  size_t last_index = 0;

  while ((index = str.find(old, last_index)) != std::string::npos) {
    result += str.substr(last_index, index - last_index);
    result += new_str;
    last_index = index + old.length();
  }

  result += str.substr(last_index, str.length() - last_index + 1);

  str = result;

  return;
}

192 193
char *bin_to_hex(const char *s, const int len, char *hex_buff)
{
羽飞's avatar
羽飞 已提交
194 195 196
  int new_len = 0;
  unsigned char *end = (unsigned char *)s + len;
  for (unsigned char *p = (unsigned char *)s; p < end; p++) {
197
    new_len += snprintf(hex_buff + new_len, 3, "%02x", *p);
羽飞's avatar
羽飞 已提交
198 199 200 201 202 203
  }

  hex_buff[new_len] = '\0';
  return hex_buff;
}

204 205
char *hex_to_bin(const char *s, char *bin_buff, int *dest_len)
{
羽飞's avatar
羽飞 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
  char buff[3];
  char *src;
  int src_len;
  char *p_dest;
  char *p_dest_end;

  src_len = strlen(s);
  if (src_len == 0) {
    *dest_len = 0;
    bin_buff[0] = '\0';
    return bin_buff;
  }

  *dest_len = src_len / 2;
  src = (char *)s;
  buff[2] = '\0';

  p_dest_end = bin_buff + (*dest_len);
  for (p_dest = bin_buff; p_dest < p_dest_end; p_dest++) {
    buff[0] = *src++;
    buff[1] = *src++;
    *p_dest = (char)strtol(buff, NULL, 16);
  }

  *p_dest = '\0';
  return bin_buff;
}

234 235
bool is_blank(const char *s)
{
L
Longda 已提交
236 237 238
  if (s == nullptr) {
    return true;
  }
羽飞's avatar
羽飞 已提交
239 240 241 242 243 244 245 246 247
  while (*s != '\0') {
    if (!isspace(*s)) {
      return false;
    }
    s++;
  }
  return true;
}

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/**
 * 获取子串
 * 从s中提取下标为n1~n2的字符组成一个新字符串,然后返回这个新串的首地址
 *
 * @param s
 * @param n1
 * @param n2
 * @return
 */
char *substr(const char *s, int n1, int n2)
{
  char *sp = (char *)malloc(sizeof(char) * (n2 - n1 + 2));
  int i, j = 0;
  for (i = n1; i <= n2; i++) {
    sp[j++] = s[i];
  }
  sp[j] = 0;
  return sp;
}

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
/**
 * double to string
 * @param v
 * @return
 */
std::string double_to_str(double v)
{
  char buf[256];
  snprintf(buf, sizeof(buf), "%.2f", v);
  size_t len = strlen(buf);
  while (buf[len - 1] == '0') {
    len--;
  }
  if (buf[len - 1] == '.') {
    len--;
  }

  return std::string(buf, len);
}
287
}  // namespace common