CARLA
 
载入中...
搜索中...
未找到
StringUtil.h
浏览该文件的文档.
1// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6//g++ -o output_file source_file.cpp -finput-charset=UTF-8 -fexec-charset=UTF-8
7// 此文件采用 UTF-8 编码,以适应多语言字符集的处理需求。
8#pragma once
9
10#include <boost/algorithm/string.hpp>
11
12namespace carla {
13// 定义名为 StringUtil 的类,用于提供各种字符串处理工具方法
14 class StringUtil {
15 public:
16// 将 const char* 类型的字符串转换为 const char*,直接返回输入参数。
17 // 在 UTF-8 编码环境下,确保对原始 const char* 字符串的正确引用。
18 static const char *ToConstCharPtr(const char *str) {
19 return str;
20 }
21 // 对于模板类型的字符串,将其转换为 const char*,通过调用其 c_str() 方法。
22 // UTF-8 编码中,此方法可处理不同类型的字符串并转换为 const char*,方便后续操作。
23 template <typename StringT>
24 static const char *ToConstCharPtr(const StringT &str) {
25 return str.c_str();
26 }
27// 判断输入范围 input 是否以范围 test 开头
28// 在 UTF-8 编码下,能够准确判断包含多语言字符的字符串是否以特定子串开头。
29 template <typename Range1T, typename Range2T>
30 static bool StartsWith(const Range1T &input, const Range2T &test) {
31 return boost::algorithm::istarts_with(input, test);
32 }
33
34 template <typename Range1T, typename Range2T>
35 static bool EndsWith(const Range1T &input, const Range2T &test) {
36 return boost::algorithm::iends_with(input, test);
37 }
38
39 template <typename WritableRangeT>
40 static void ToLower(WritableRangeT &str) {
41 boost::algorithm::to_lower(str);
42 }
43// 将可写范围的字符串转换为小写形式
44// UTF-8 编码下,为不可修改的字符串提供小写形式的副本,适用于多语言环境。
45 template <typename SequenceT>
46 static auto ToLowerCopy(const SequenceT &str) {
47 return boost::algorithm::to_lower_copy(str);
48 }
49// 将可写范围的字符串转换为大写形式
50// UTF-8 编码中,对不同语言字符的字符串进行大写转换操作。
51 template <typename WritableRangeT>
52 static void ToUpper(WritableRangeT &str) {
53 boost::algorithm::to_upper(str);
54 }
55 // 将传入的字符串序列转换为大写副本并返回
56 template <typename SequenceT>
57 static auto ToUpperCopy(const SequenceT &str) {
58 return boost::algorithm::to_upper_copy(str);
59 }
60 // 去除可写范围字符串两端的空白字符
61 // UTF-8 编码环境下,有效去除多语言字符串两端的空白字符。
62 template <typename WritableRangeT>
63 static void Trim(WritableRangeT &str) {
64 boost::algorithm::trim(str);
65 }
66// 对于不可写范围的字符串,返回去除两端空白字符后的副本
67// 在 UTF-8 编码中,为不可修改的字符串提供去除空白后的副本,适用于各种语言。
68 template <typename SequenceT>
69 static auto TrimCopy(const SequenceT &str) {
70 return boost::algorithm::trim_copy(str);
71 }
72// 将字符串 str 按照分隔符集合 separators 进行分割,结果存储在 destination 容器中
73// UTF-8 编码下,能正确分割包含多语言字符的字符串。
74 template<typename Container, typename Range1T, typename Range2T>
75 static void Split(Container &destination, const Range1T &str, const Range2T &separators) {
76 boost::split(destination, str, boost::is_any_of(separators));
77 }
78
79 /// Match @a str with the Unix shell-style @a wildcard_pattern.
80 // 匹配 str 与 Unix shell 风格的通配符模式 wildcard_pattern。
81 // 在 UTF-8 编码中,尝试对不同语言字符的字符串进行通配符匹配。
82 static bool Match(const char *str, const char *wildcard_pattern);
83
84 /// Match @a str with the Unix shell-style @a wildcard_pattern.
85 // 模板版本的 Match 方法,对于不同类型的字符串进行转换后调用底层的 Match 方法。
86 // 在 UTF-8 编码场景下,确保不同类型字符串能正确与通配符模式匹配。
87 template <typename String1T, typename String2T>
88 static bool Match(const String1T &str, const String2T &wildcard_pattern) {
89 return Match(ToConstCharPtr(str), ToConstCharPtr(wildcard_pattern));
90 }
91 };
92
93} // namespace carla
static void ToUpper(WritableRangeT &str)
Definition StringUtil.h:52
static bool Match(const char *str, const char *wildcard_pattern)
Match str with the Unix shell-style wildcard_pattern.
static void Split(Container &destination, const Range1T &str, const Range2T &separators)
Definition StringUtil.h:75
static bool Match(const String1T &str, const String2T &wildcard_pattern)
Match str with the Unix shell-style wildcard_pattern.
Definition StringUtil.h:88
static const char * ToConstCharPtr(const char *str)
Definition StringUtil.h:18
static auto ToLowerCopy(const SequenceT &str)
Definition StringUtil.h:46
static auto ToUpperCopy(const SequenceT &str)
Definition StringUtil.h:57
static auto TrimCopy(const SequenceT &str)
Definition StringUtil.h:69
static const char * ToConstCharPtr(const StringT &str)
Definition StringUtil.h:24
static bool EndsWith(const Range1T &input, const Range2T &test)
Definition StringUtil.h:35
static void Trim(WritableRangeT &str)
Definition StringUtil.h:63
static void ToLower(WritableRangeT &str)
Definition StringUtil.h:40
static bool StartsWith(const Range1T &input, const Range2T &test)
Definition StringUtil.h:30
CARLA模拟器的主命名空间。
Definition Carla.cpp:139