M5Utility 0.0.2 git rev:5c1a751
Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UTILITY_STRING_HPP
11#define M5_UTILITY_STRING_HPP
12
13#include <string>
14#include <cstdint>
15
16namespace m5 {
17namespace utility {
18
20std::string formatString(const char* fmt, ...);
21
25
26std::string& trimRight(std::string& s);
28std::string& trimLeft(std::string& s);
30std::string& trim(std::string& s);
32
35
39template <bool Case = true>
40constexpr char uintToHexChar(const uint8_t v)
41{
42 return (v & 0x0F) < 10 ? '0' + (v & 0x0F) : (Case ? 'A' : 'a') + ((v & 0x0F) - 10);
43}
44
49template <typename T, bool Case = true>
50std::string unsignedToHexString(const T& v)
51{
52 static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value, "T must be unsigned integer");
53 std::string s;
54 for (size_t i = sizeof(T); i > 0; --i) {
55 uint8_t u8 = (v >> ((i - 1) * 8)) & 0xFF;
56 s += uintToHexChar<Case>((u8 >> 4) & 0x0F);
57 s += uintToHexChar<Case>(u8 & 0x0F);
58 }
59 return s;
60}
62
63} // namespace utility
64} // namespace m5
65#endif
Top level namespace of M5.
Definition bit_segment.hpp:17
For utilities.
std::string formatString(const char *fmt,...)
@ brief Create a string in a format similar to printf
Definition string.cpp:17
std::string & trim(std::string &s)
Trim both ends.
Definition string.cpp:47
std::string & trimRight(std::string &s)
Trim right.
Definition string.cpp:34
std::string & trimLeft(std::string &s)
Trim left.
Definition string.cpp:40
constexpr char uintToHexChar(const uint8_t v)
Convert from 0~15 to hexadecimal character.
Definition string.hpp:40
std::string unsignedToHexString(const T &v)
Convert any one unsigned integer to a hexadecimal string.
Definition string.hpp:50