# Strings

> Text goes in as a std::string_view and comes out as a std::string, never as a C string; how it reaches raylib's C without reading past its end; and what builds text in a game the oldest compiler can still build.

Source: https://omardev29.github.io/rmp-docs/guidelines/strings.html

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`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#load_texture), [`input::pressed`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-input.html#pressed), [`audio::play`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html#play), every label of [`rmp::ui`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ui.html#button) — and `tools/pointer_check.py` refuses a C string in any declaration a game can name:

Compiles; `rmp test pointers` refuses it

```cpp
#pragma once

namespace rmp {
void say(const char *line);
} // namespace rmp
```

```cpp
#pragma once

#include <string_view>

namespace rmp {
void say(std::string_view line);
} // namespace rmp
```

Outside the public headers a `const char *` is a raw pointer like any other, and clang-tidy refuses it as one ([Pointers and RAII](https://omardev29.github.io/rmp-docs/guidelines/pointers-and-raii.html)). 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()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-save.html#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()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Value.html#as_string) hands back is good while that value is alive and unchanged, and the one [`Sprite::playing()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#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:

Compiles; `rmp lint` refuses it: `bugprone-dangling-handle`

```cpp
std::string_view title_for(const int level) {
    const std::string title = "Level " + std::to_string(level);
    return title;
}
```

```cpp
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()`:

Does not compile

```cpp
void say(std::string_view line) {
    DrawText(line, 10, 10, 20, WHITE);
}
```

The compiler refuses it, saying among other things: `const char`

Compiles; `rmp lint` refuses it: `bugprone-suspicious-stringview-data-usage`

```cpp
void say(std::string_view line) {
    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.

```cpp
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:

```cpp
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`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Value.html) takes a literal, a `std::string` and a view, and does not take a `const char *`, which might be null:

Does not compile

```cpp
rmp::Value settings = rmp::Value::object();
settings["folder"] = GetWorkingDirectory();
```

The compiler refuses it, saying among other things: `const char`

```cpp
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.

```cpp
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](https://omardev29.github.io/rmp-docs/guidelines/modern-cpp.html#floor), 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`:

Compiles; `rmp test style` refuses it: `S4`

```cpp
#include <format>
#include <string>

std::string lives_text(const int lives) { return std::format("{} lives", lives); }
```

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.

Compiles; `rmp test style` refuses it: `S5`

```cpp
using namespace std::string_view_literals;
constexpr auto TITLE = "Pong"sv;
```

```cpp
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](https://omardev29.github.io/rmp-docs/guidelines/index.html#switching-on-clang-tidy). [What checks what](https://omardev29.github.io/rmp-docs/guidelines/enforcement.html) has every rule of these pages in one table.
