abseil-faster-strsplit-delimiter

查找使用 absl::StrSplit()absl::MaxSplits() 且分隔符为单个字符字符串字面量的实例,并用字符替换。该检查将提供将字符串字面量更改为字符的建议。它还会捕获使用 absl::ByAnyChar() 仅用于单个字符的代码,并将该代码也转换为单个字符。

这些更改将产生相同的结果,但使用字符而不是单个字符字符串字面量更有效率且可读性更高。

示例

// Original - the argument is a string literal.
for (auto piece : absl::StrSplit(str, "B")) {

// Suggested - the argument is a character, which causes the more efficient
// overload of absl::StrSplit() to be used.
for (auto piece : absl::StrSplit(str, 'B')) {


// Original - the argument is a string literal inside absl::ByAnyChar call.
for (auto piece : absl::StrSplit(str, absl::ByAnyChar("B"))) {

// Suggested - the argument is a character, which causes the more efficient
// overload of absl::StrSplit() to be used and we do not need absl::ByAnyChar
// anymore.
for (auto piece : absl::StrSplit(str, 'B')) {


// Original - the argument is a string literal inside absl::MaxSplits call.
for (auto piece : absl::StrSplit(str, absl::MaxSplits("B", 1))) {

// Suggested - the argument is a character, which causes the more efficient
// overload of absl::StrSplit() to be used.
for (auto piece : absl::StrSplit(str, absl::MaxSplits('B', 1))) {