-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathstring-utils.h
More file actions
62 lines (51 loc) · 1.32 KB
/
Copy pathstring-utils.h
File metadata and controls
62 lines (51 loc) · 1.32 KB
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
// Copyright (C) 2022 Satya Das and CppParser contributors
// SPDX-License-Identifier: MIT
#ifndef D8D07072_99E2_4590_8529_DC100184F9BB
#define D8D07072_99E2_4590_8529_DC100184F9BB
#include <algorithm>
#include <cstring>
#include <string>
inline size_t StripChar(char* s, size_t len, char c)
{
auto* end = std::remove(s, s + len, c);
return end - s;
}
inline void StripChar(std::string& s, char c)
{
auto len = StripChar(&s[0], s.length(), c);
s.resize(len);
}
inline std::string& TrimBlob(std::string& s)
{
auto len = s.size();
for (; len > 0; --len)
{
if (!isspace(s[len - 1]))
break;
}
s.resize(len);
size_t start = 0;
for (size_t i = 0; i < s.size(); ++i)
{
if (!isspace(s[i]))
break;
if (s[i] == '\n')
start = i + 1;
}
if (start > 0)
s = s.substr(start);
return s;
}
//! strips new-line char and collapses multiple white chars.
inline std::string& CleanseIdentifier(std::string& id)
{
StripChar(id, '\n');
auto end = std::unique(id.begin(), id.end(), [](char c1, char c2) {
return ((c1 == ' ' && c2 == ' ') || (c1 == '\t' && c2 == '\t') || (c1 == ' ' && c2 == '\t')
|| (c1 == '\t' && c2 == ' '));
});
id.resize(end - id.begin());
std::replace(id.begin(), id.end(), '\t', ' ');
return id;
}
#endif /* D8D07072_99E2_4590_8529_DC100184F9BB */