Regular Literal Strings for Erbsland Core APIs
- Rule ID:
regular_string_literal- Severity:
medium
At many locations, regular string literals are used for APIs that take a String argument instead of an Erbsland Core
string literal (""_el).
This looks like a minor issue, but it has a significant performance and memory impact.
Regular string literals always cause a copy of the text, while Erbsland Core string literals are used as no-copy references to read-only memory. They avoid both the copy and a heap allocation. This is especially important when throwing exceptions or comparing against static text values.
Scope and Exceptions
In headers and templates,
throw...Errorandthrow...Failurewrappers takingstd::string_vieware accepted inside theerbsland::textnamespace for string methods that would create a circular dependency. Both the reason and parameter-name literals are accepted forthrowParameterFailure(reason, parameterName)andParameterFailure{reason, parameterName}.Files with
WindowsorPosixin their names are centrally excluded from this rule because they primarily contain native API integration code where narrow C strings are expected. These files still require manual inspection.Plain narrow literals are also correct inside
static_assertandassertexpressions and as messages in the standarddeprecatedattribute.
Correct Solution
Add
using namespace text::literal;inside the primary namespace bracket of acppfile.In
hppand template files, add the using directive inside the function. Inimplnamespaces, it can be added directly to the namespace, as incppfiles.Add the
_elsuffix to all string literals where possible.An explicit direct construction, such as
StringLiteral{"text"},U8StringLiteral<char>{"text"}, or another type that deliberately accepts a C string, is also valid and does not require an_elsuffix.
Mechanical Detection
The scanner reports likely unsuffixed narrow string literals. Inspect each reported location manually.