1 // Copyright 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/string_utils.h"
16
17 #include <iostream>
18 #include <sstream>
19 #include <string>
20 #include <string_view>
21 #include <vector>
22
23 namespace netsim {
24 namespace stringutils {
25 namespace {
26
27 const std::string WHITESPACE = " \n\r\t\f\v";
28 const char hex_digits[] = "0123456789ABCDEF";
29
30 } // namespace
31
LTrim(const std::string_view s)32 std::string_view LTrim(const std::string_view s) {
33 auto start = s.find_first_not_of(WHITESPACE);
34 return (start == std::string::npos) ? "" : s.substr(start);
35 }
36
RTrim(const std::string_view s)37 std::string_view RTrim(const std::string_view s) {
38 size_t end = s.find_last_not_of(WHITESPACE);
39 return (end == std::string::npos) ? "" : s.substr(0, end + 1);
40 }
41
Trim(const std::string_view s)42 std::string_view Trim(const std::string_view s) { return RTrim(LTrim(s)); }
43
Split(const std::string_view s,const std::string_view & delimiters)44 std::vector<std::string_view> Split(const std::string_view s,
45 const std::string_view &delimiters) {
46 std::vector<std::string_view> result;
47 size_t start, end = 0;
48 while ((start = s.find_first_not_of(delimiters, end)) != std::string::npos) {
49 end = s.find(delimiters, start);
50 result.emplace_back(s.substr(start, end - start));
51 }
52 return result;
53 }
54
Split(const std::string s,const std::string & delimiters)55 std::vector<std::string> Split(const std::string s,
56 const std::string &delimiters) {
57 std::vector<std::string> result;
58 size_t start, end = 0;
59 while ((start = s.find_first_not_of(delimiters, end)) != std::string::npos) {
60 end = s.find(delimiters, start);
61 result.emplace_back(s.substr(start, end - start));
62 }
63 return result;
64 }
65
ToHexString(uint8_t x,uint8_t y)66 std::string ToHexString(uint8_t x, uint8_t y) {
67 return {'0',
68 'x',
69 hex_digits[x >> 4],
70 hex_digits[x & 0x0f],
71 hex_digits[y >> 4],
72 hex_digits[y & 0x0f]};
73 }
74
ToHexString(uint8_t x)75 std::string ToHexString(uint8_t x) {
76 return {'0', 'x', hex_digits[x >> 4], hex_digits[x & 0x0f]};
77 }
78
ToHexString(const uint8_t * buf,size_t len)79 std::string ToHexString(const uint8_t *buf, size_t len) {
80 std::stringstream ss;
81 for (int i = 0; i < len; i++) {
82 ss << hex_digits[buf[i] >> 4] << hex_digits[buf[i] & 0x0f];
83 if (i < len - 1) {
84 ss << " ";
85 }
86 }
87 return ss.str();
88 }
89
ToHexString(const std::vector<uint8_t> & data,int max_length)90 std::string ToHexString(const std::vector<uint8_t> &data, int max_length) {
91 std::string result;
92 auto length = max_length > data.size() ? data.size() : max_length;
93 result.reserve(3 * length + 1);
94 for (auto iter = data.begin(); iter < data.begin() + length; iter++) {
95 result.push_back(hex_digits[*iter >> 4]);
96 result.push_back(hex_digits[*iter & 0x0f]);
97 result.push_back(' ');
98 }
99 if (result.length() > 0) {
100 result.pop_back();
101 }
102 return result;
103 }
104
105 } // namespace stringutils
106 } // namespace netsim
107