MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
string.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11//
12// Created by Longda on 2010
13//
14
15#pragma once
16
17// Basic includes
18#include <cxxabi.h>
19#include <signal.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include <cstdlib>
24#include <string>
25#include <typeinfo>
26
27#include "common/defs.h"
28#include "common/lang/vector.h"
29#include "common/lang/iostream.h"
30#include "common/lang/sstream.h"
31#include "common/lang/set.h"
32
33using std::stof;
34using std::stol;
35using std::string;
36using std::to_string;
37
38namespace common {
39
43void strip(string &str);
44char *strip(char *str);
45
52string size_to_pad_str(int size, int pad);
53
59string &str_to_upper(string &s);
60
66string &str_to_lower(string &s);
67
74void split_string(const string &str, string delim, set<string> &results);
75void split_string(const string &str, string delim, vector<string> &results);
76void split_string(char *str, char dim, vector<char *> &results, bool keep_null = false);
77
78void merge_string(string &str, string delim, vector<string> &result, size_t result_len = 0);
82void replace(string &str, const string &old, const string &new_str);
83
87char *bin_to_hex(const char *s, const int len, char *hex_buff);
91char *hex_to_bin(const char *s, char *bin_buff, int *dest_len);
92
105template <class T>
106bool str_to_val(const string &str, T &val, ios_base &(*radix)(ios_base &) = std::dec);
107
118template <class T>
119void val_to_str(const T &val, string &str, ios_base &(*radix)(ios_base &) = std::dec);
120
126string double_to_str(double v);
127
128bool is_blank(const char *s);
129
139char *substr(const char *s, int n1, int n2);
140
144template <class T>
145string get_type_name(const T &val);
146
147template <class T>
148bool str_to_val(const string &str, T &val, ios_base &(*radix)(ios_base &)/* = std::dec */)
149{
150 bool success = true;
151 istringstream is(str);
152 if (!(is >> radix >> val)) {
153 val = 0;
154 success = false;
155 }
156 return success;
157}
158
159template <class T>
160void val_to_str(const T &val, string &str, ios_base &(*radix)(ios_base &)/* = std::dec */)
161{
162 stringstream strm;
163 strm << radix << val;
164 str = strm.str();
165}
166
167template <class T>
168string get_type_name(const T &val)
169{
170 int status = 0;
171 char *stmp = abi::__cxa_demangle(typeid(val).name(), 0, 0, &status);
172 if (!stmp)
173 return "";
174
175 string sret(stmp);
176
177 ::free(stmp);
178 return sret;
179}
180
181} // namespace common