Strings
Text goes into a function as a std::string_view and comes out of one
as a std::string โ or as a view whose comment says how long it is good for. No
declaration a game can name has a C string in it. Where text has to reach raylib, which is C and
wants a const char * that ends in a zero, it goes through a std::string
and never through a view's data().
In: std::string_view#
One parameter type takes a literal, a std::string and another view, and copies
none of them. A const char * may be null, and has to end in a zero nobody can see; a
const std::string & turns every literal handed to it into a
std::string first. So the framework's functions that take a name take a view โ
load_texture,
input::pressed,
audio::play, every label of
rmp::ui โ and
tools/pointer_check.py refuses a C string in any declaration a game can name:
rmp test pointers refuses it#pragma once
namespace rmp {
void say(const char *line);
} // namespace rmpFAIL: include/rmp/snippet.h:4: rmp::say -- parameter line `const char *` FAIL: a raw pointer or a C string in rmp's API: a value, a reference, std::span, std::optional, std::string_view in and std::string out, rmp::Ref for an optional reference, rmp::Handle across frames.
#pragma once
#include <string_view>
namespace rmp {
void say(std::string_view line);
} // namespace rmpOutside the public headers a const char * is a raw pointer like any other, and
clang-tidy refuses it as one (Pointers and
RAII). A const std::string & parameter passes every tool; it is a question
for review.
Out: a std::string, or a view that says how long#
A function that makes text hands it back as a std::string, which owns it:
save::directory() does. A view handed back
is a view of something that lives elsewhere, so it is only right when that something outlives the
call, and its comment says until when.
The
view Value::as_string() hands back is good
while that value is alive and unchanged, and the one
Sprite::playing() hands back for as long as
the sheet is loaded; each comment says so. A view of a std::string that dies at the end of the line,
or of the function, is refused:
rmp lint refuses it: bugprone-dangling-handlestd::string_view title_for(const int level) {
const std::string title = "Level " + std::to_string(level);
return title;
}snippet.cpp:3:5: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
3 | return title;
| ^std::string title_for(const int level) { return "Level " + std::to_string(level); }To raylib: through a std::string#
raylib's text functions take a const char * and read until they find a zero. A
std::string_view has no zero after its last character: it may be a piece of a longer
text, and its data() carries on into whatever comes next. The compiler will not turn
a view into a pointer, so the mistake is to reach for data():
void say(std::string_view line) {
DrawText(line, 10, 10, 20, WHITE);
}snippet.cpp:2:5: error: no matching function for call to 'DrawText'
2 | DrawText(line, 10, 10, 20, WHITE);
| ^~~~~~~~
thirdparty/raylib/src/raylib.h:1524:12: note: candidate function not viable: no known conversion from 'std::string_view' (aka 'basic_string_view<char>') to 'const char *' for 1st argument
1524 | RLAPI void DrawText(const char *text, int posX, int posY, int fontSize, Color color); // Draw text (using default font)
| ^ ~~~~~~~~~~~~~~~~rmp lint refuses it: bugprone-suspicious-stringview-data-usagevoid say(std::string_view line) {
DrawText(line.data(), 10, 10, 20, WHITE);
}snippet.cpp:2:19: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues [bugprone-suspicious-stringview-data-usage]
2 | DrawText(line.data(), 10, 10, 20, WHITE);
| ~~~~~^~~~A std::string
made from the view ends in a zero, and lives until the end of the statement: long enough for the
call. The framework passes a name to raylib this way.
void say(std::string_view line) {
DrawText(std::string(line).c_str(), 10, 10, 20, WHITE);
}The UI takes the other way out: it copies every piece of text it is given into memory of its own that lasts until the next frame, because the layout library under it keeps the pointer it is given and reads it later. So a temporary is safe there:
rmp::ui::begin();
rmp::ui::text(std::to_string(score));
rmp::ui::end();Text from raylib comes back the other way as a const char *, and goes into a
std::string before it goes anywhere else. A rmp::Value
takes a literal, a std::string and a view, and does not take a
const char *, which might be null:
rmp::Value settings = rmp::Value::object();
settings["folder"] = GetWorkingDirectory();snippet.cpp:2:20: error: no viable overloaded '='
2 | settings["folder"] = GetWorkingDirectory();
| ~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~
include/rmp/save.h:265:10: note: candidate function not viable: no known conversion from 'const char *' to 'Value' for 1st argument
265 | Ref &operator=(Value value);
| ^ ~~~~~~~~~~~rmp::Value settings = rmp::Value::object();
settings["folder"] = std::string(GetWorkingDirectory());Building text#
With what every compiler a game is built with has: std::to_string and
+ for a std::string, and raylib's TextFormat for a line to
draw.
std::string lives_text(const int lives) { return std::to_string(lives) + " lives"; }
void draw_lives(const int lives) {
DrawText(TextFormat("%d lives", lives), 10, 10, 20, WHITE);
}Not
<format>: the oldest compiler a game
is built with, NetBSD's GCC 10.5, does not have it. It compiles on a newer one, so tools/style_check.sh refuses it, in every
branch of every #if:
rmp test style refuses it: S4#include <format>
#include <string>
std::string lives_text(const int lives) { return std::format("{} lives", lives); }FAIL snippet.cpp:1: S4 #include <format> -- newer than GCC 10.5, the floor compiler (NetBSD), in code a game compiles
And
not "Pong"sv: Google's guide has no user-defined literals, the standard library's
included, and neither does the framework. A std::string_view constant is spelt out,
and a literal turns into one by itself.
rmp test style refuses it: S5using namespace std::string_view_literals;
constexpr auto TITLE = "Pong"sv;FAIL snippet.cpp:1: S5 using namespace std::string_view_literals -- a user-defined literal: Google's style has none, the standard library's included
constexpr std::string_view TITLE = "Pong";Who checks what#
| Rule | Tool | Where |
|---|---|---|
| No C string in a declaration a game can name | tools/pointer_check.py, rmp test pointers | the declarations in include/rmp/ |
No const char * anywhere else | custom-rmp-raw-pointer; rmp lint, against tools/lint_debt.txt | src/, tests/, examples/ |
| No view that outlives what it views | bugprone-dangling-handle; rmp lint | src/, tests/, examples/ |
No data() of a view where a C string is wanted | bugprone-suspicious-stringview-data-usage; rmp lint | src/, tests/, examples/ |
No <format>, no user-defined literal | tools/style_check.sh S4 and S5, rmp test style | include/, src/ and examples/; S5 in tests/ too |
A game gets none of these tools: in a game, these rules are a recommendation, and the clang-tidy checks among them hold it only if it switches the framework's clang-tidy on. What checks what has every rule of these pages in one table.