WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <cstring>
8#include <string_view>
9
10#include "wpi/util/string.h"
11
12namespace wpi::util {
13
14/** Converts a WPI_String to a string_view */
15constexpr std::string_view to_string_view(const struct WPI_String* str) {
16 if (str) {
17 return {str->str, str->len};
18 } else {
19 return "";
20 }
21}
22
23/** Converts a string_view to a WPI_String */
24constexpr WPI_String make_string(std::string_view view) {
25 return WPI_String{view.data(), view.size()};
26}
27
28/** Allocates a copy of a string_view and stores the result into a WPI_String */
29inline WPI_String alloc_wpi_string(std::string_view view) {
30 WPI_String out;
31 size_t len = view.size();
32 std::memcpy(WPI_AllocateString(&out, len), view.data(), len);
33 return out;
34}
35
36/** Allocates a copy of a WPI_String */
38 if (str.str == nullptr || str.len == 0) {
39 return WPI_String{nullptr, 0};
40 }
41 return alloc_wpi_string(to_string_view(&str));
42}
43
44} // namespace wpi::util
Definition raw_os_ostream.hpp:19
constexpr std::string_view to_string_view(const struct WPI_String *str)
Converts a WPI_String to a string_view.
Definition string.hpp:15
WPI_String copy_wpi_string(const WPI_String &str)
Allocates a copy of a WPI_String.
Definition string.hpp:37
constexpr WPI_String make_string(std::string_view view)
Converts a string_view to a WPI_String.
Definition string.hpp:24
WPI_String alloc_wpi_string(std::string_view view)
Allocates a copy of a string_view and stores the result into a WPI_String.
Definition string.hpp:29
char * WPI_AllocateString(struct WPI_String *wpiString, size_t length)
Allocates a WPI_String for the specified length.
A const UTF8 string.
Definition string.h:12
const char * str
Contents.
Definition string.h:14
size_t len
Length.
Definition string.h:16