|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2018 [email protected]
4: //
5:
6: #include "header.h"
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 root 66: #if 0
67: #include <cstdio>
68: #include <sys/time.h>
69: int main()
70: {
71: std::string s0 = string_format("%d_%d", 0, 1);
72: if (s0 != "0_1") {
73: printf("error\n");
74: return 1;
75: }
76: timeval start, end, result;
77: gettimeofday(&start, NULL);
78: for (int i = 0; i < 100000; i++) {
79: std::string s = string_format("%d_%d", i, i);
80: }
81: gettimeofday(&end, NULL);
82: timersub(&end, &start, &result);
83: long long t = (result.tv_sec) * 1000000 + result.tv_usec;
84: printf("%d.%03d\n", (int)t / 1000, (int)t % 1000);
85: return 0;
86: }
87: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.