string.cpp 5.1 KB
Newer Older
羽飞's avatar
羽飞 已提交
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 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
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 {

char *strip(char *str_) {
  if (str_ == NULL || *str_ == 0){
    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;
}

void strip(std::string &str) {
  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
std::string size_to_pad_str(int size, int pad) {
  std::ostringstream ss;
  ss << std::setw(pad) << std::setfill('0') << size;
  return ss.str();
}

std::string &str_to_upper(std::string &s) {
  std::transform(s.begin(), s.end(), s.begin(), (int (*)(int)) & std::toupper);
  return s;
}

std::string &str_to_lower(std::string &s) {
  std::transform(s.begin(), s.end(), s.begin(), (int (*)(int)) & std::tolower);
  return s;
}

void split_string(const std::string &str, std::string delim,
                 std::set<std::string> &results) {
  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);
  }
}

void split_string(const std::string &str, std::string delim,
                 std::vector<std::string> &results) {
  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);
  }
}

void split_string(char *str, char dim, std::vector<char *> &results,
                 bool keep_null) {
  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;
}

void merge_string(std::string &str, std::string delim,
                 std::vector<std::string> &source, size_t result_len){

  std::ostringstream ss;
  if (source.empty() ) {
    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];
    }else {
      ss << delim << source[i];
    }

  }

  str = ss.str();
  return ;
}

void replace(std::string &str, const std::string &old,
             const std::string &new_str) {
  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;
}

char *bin_to_hex(const char *s, const int len, char *hex_buff) {
  int new_len = 0;
  unsigned char *end = (unsigned char *)s + len;
  for (unsigned char *p = (unsigned char *)s; p < end; p++) {
    new_len += sprintf(hex_buff + new_len, "%02x", *p);
  }

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

char *hex_to_bin(const char *s, char *bin_buff, int *dest_len) {
  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;
}

bool is_blank(const char *s) {
  while (*s != '\0') {
    if (!isspace(*s)) {
      return false;
    }
    s++;
  }
  return true;
}

} //namespace common