|
|
1.1 root 1: //
2: // nono
1.1.1.3 root 3: // Copyright (C) 2020 nono project
4: // Licensed under nono-license.txt
1.1 root 5: //
6:
7: #include "mystring.h"
1.1.1.2 root 8: #include <algorithm>
1.1 root 9:
10: std::string
11: string_format(const char *fmt, ...)
12: {
13: va_list ap;
14: char *buf;
15:
16: va_start(ap, fmt);
17: vasprintf(&buf, fmt, ap);
18: va_end(ap);
19: std::string rv(buf);
20: free(buf);
21:
22: return rv;
23: }
24:
1.1.1.2 root 25: // 文字列 str から先頭の連続する空白文字を取り除いた新しい文字列を返す。
26: std::string
27: string_ltrim(const std::string& str)
28: {
29: auto it = str.begin();
30: for (; it != str.end(); it++) {
31: if (!isspace(*it))
32: break;
33: }
34: return std::string(it, str.end());
35: }
36:
37: // 文字列 str から末尾の連続する空白文字を取り除く (str を書き換える)。
38: void
39: string_rtrim(std::string& str)
40: {
41: while (isspace(*str.rbegin())) {
42: str.pop_back();
43: }
44: }
45:
46: // 文字列 str から末尾の連続する空白文字を取り除く (str を書き換える)。
47: void
48: rtrim(char *str)
49: {
50: char *p = strchr(str, '\0');
51: while (--p >= str && (isspace((int)*p) || *p == '\r' || *p == '\n')) {
52: *p = '\0';
53: }
54: }
55:
56: // 文字列 src 中の ASCII 大文字を小文字にした新しい文字列を返す。
57: std::string
58: string_tolower(const std::string& src)
59: {
60: std::string dst(src);
61: std::transform(dst.begin(), dst.end(), dst.begin(),
62: [](unsigned char c){ return std::tolower(c); });
63: return dst;
64: }
65:
1.1.1.4 ! root 66: // 文字列 lhs の先頭が rhs と大文字小文字の区別なしで一致すれば true を返す。
! 67: // ASCII 専用。
! 68: // C++20 の starts_with に似せておく。
! 69: bool
! 70: starts_with_ignorecase(const std::string& lhs, const std::string& rhs)
! 71: {
! 72: if (lhs.length() < rhs.length()) {
! 73: return false;
! 74: }
! 75: #if 0
! 76: return std::equal(
! 77: lhs.begin(), lhs.begin() + rhs.length(),
! 78: rhs.begin(),
! 79: [](std::string::value_type l, std::string::value_type r) {
! 80: return std::tolower(l) == std::tolower(r);
! 81: }
! 82: );
! 83: #else
! 84: // こっちのほうが分かりやすいよな
! 85: return strncasecmp(lhs.c_str(), rhs.c_str(), rhs.length()) == 0;
! 86: #endif
! 87: }
! 88:
! 89: // 文字列 str (長さ len) を文字 c で分割したリストを返す。
! 90: std::vector<std::string>
! 91: string_split(const char *str, int len, char c)
! 92: {
! 93: std::vector<std::string> list;
! 94:
! 95: for (int pos = 0, end = 0 ; pos < len; pos = end + 1) {
! 96: const char *p = strchr(str + pos, c);
! 97: if (p) {
! 98: end = p - str;
! 99: } else {
! 100: end = len;
! 101: }
! 102:
! 103: list.emplace_back(str + pos, end - pos);
! 104: }
! 105:
! 106: return list;
! 107: }
! 108:
1.1 root 109: #if 0
110: #include <cstdio>
111: #include <sys/time.h>
112: int main()
113: {
114: std::string s0 = string_format("%d_%d", 0, 1);
115: if (s0 != "0_1") {
116: printf("error\n");
117: return 1;
118: }
119: timeval start, end, result;
120: gettimeofday(&start, NULL);
121: for (int i = 0; i < 100000; i++) {
122: std::string s = string_format("%d_%d", i, i);
123: }
124: gettimeofday(&end, NULL);
125: timersub(&end, &start, &result);
126: long long t = (result.tv_sec) * 1000000 + result.tv_usec;
127: printf("%d.%03d\n", (int)t / 1000, (int)t % 1000);
128: return 0;
129: }
130: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.