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
ToHexString(uint8_t x,uint8_t y)55 std::string ToHexString(uint8_t x, uint8_t y) {
56 return {'0',
57 'x',
58 hex_digits[x >> 4],
59 hex_digits[x & 0x0f],
60 hex_digits[y >> 4],
61 hex_digits[y & 0x0f]};
62 }
63
ToHexString(uint8_t x)64 std::string ToHexString(uint8_t x) {
65 return {'0', 'x', hex_digits[x >> 4], hex_digits[x & 0x0f]};
66 }
67
ToHexString(const uint8_t * buf,size_t len)68 std::string ToHexString(const uint8_t *buf, size_t len) {
69 std::stringstream ss;
70 for (int i = 0; i < len; i++) {
71 ss << hex_digits[buf[i] >> 4] << hex_digits[buf[i] & 0x0f];
72 if (i < len - 1) {
73 ss << " ";
74 }
75 }
76 return ss.str();
77 }
78
ToHexString(const std::vector<uint8_t> & data,int max_length)79 std::string ToHexString(const std::vector<uint8_t> &data, int max_length) {
80 std::string result;
81 auto length = max_length > data.size() ? data.size() : max_length;
82 result.reserve(3 * length + 1);
83 for (auto iter = data.begin(); iter < data.begin() + length; iter++) {
84 result.push_back(hex_digits[*iter >> 4]);
85 result.push_back(hex_digits[*iter & 0x0f]);
86 result.push_back(' ');
87 }
88 if (result.length() > 0) {
89 result.pop_back();
90 }
91 return result;
92 }
93
94 } // namespace stringutils
95 } // namespace netsim
96