WPILibC++ 2027.0.0-alpha-3
Loading...
Searching...
No Matches
string.h
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#ifdef __cplusplus
8#include <cstring>
9#include <string_view>
10#endif
11
12/**
13 * A const UTF8 string.
14 */
15struct WPI_String {
16 /** Contents. */
17 const char* str;
18 /** Length */
19 size_t len;
20};
21
22#ifdef __cplusplus
23namespace wpi {
24/** Converts a WPI_String to a string_view */
25constexpr std::string_view to_string_view(const struct WPI_String* str) {
26 if (str) {
27 return {str->str, str->len};
28 } else {
29 return "";
30 }
31}
32
33/** Converts a string_view to a WPI_String */
34constexpr WPI_String make_string(std::string_view view) {
35 return WPI_String{view.data(), view.size()};
36}
37} // namespace wpi
38#endif // __cplusplus
39
40#ifdef __cplusplus
41extern "C" {
42#endif // __cplusplus
43
44/**
45 * Initializes a WPI_String from a null terminated UTF-8 string.
46 * If input string is null, initializes output to 0 length.
47 * The output length does not include the null terminator.
48 *
49 * The lifetime of the output string is the lifetime of the input string.
50 * Do not call WPI_FreeString() with the output of this call.
51 *
52 * @param wpiString output string
53 * @param utf8String input string (null terminated)
54 */
55void WPI_InitString(struct WPI_String* wpiString, const char* utf8String);
56
57/**
58 * Initializes a WPI_String from a UTF-8 string and length.
59 * If input string is null or 0 length, initializes output to 0 length.
60 * The input string does not need to be null terminated.
61 *
62 * The lifetime of the output string is the lifetime of the input string.
63 * Do not call WPI_FreeString() with the output of this call.
64 *
65 * @param wpiString output string
66 * @param utf8String input string
67 * @param length input string length in chars
68 */
69void WPI_InitStringWithLength(struct WPI_String* wpiString,
70 const char* utf8String, size_t length);
71
72/**
73 * Allocates a WPI_String for the specified length.
74 * The resultant string must be freed with WPI_FreeString().
75 *
76 * @param wpiString output string
77 * @param length string length in chars to allocate
78 * @return mutable pointer to allocated buffer
79 *
80 */
81char* WPI_AllocateString(struct WPI_String* wpiString, size_t length);
82
83/**
84 * Frees a WPI_String that was allocated with WPI_AllocateString()
85 *
86 * @param wpiString string to free
87 */
88void WPI_FreeString(const struct WPI_String* wpiString);
89
90/**
91 * Allocates an array of WPI_Strings.
92 *
93 * @param length array length
94 * @return string array
95 */
96struct WPI_String* WPI_AllocateStringArray(size_t length);
97
98/**
99 * Frees a WPI_String array returned by WPI_AllocateStringArray().
100 *
101 * @param wpiStringArray string array to free
102 * @param length length of array
103 */
104void WPI_FreeStringArray(const struct WPI_String* wpiStringArray,
105 size_t length);
106
107#ifdef __cplusplus
108} // extern "C"
109#endif // __cplusplus
110
111#ifdef __cplusplus
112namespace wpi {
113
114/** Allocates a copy of a string_view and stores the result into a WPI_String */
115inline WPI_String alloc_wpi_string(std::string_view view) {
116 WPI_String out;
117 size_t len = view.size();
118 std::memcpy(WPI_AllocateString(&out, len), view.data(), len);
119 return out;
120}
121
122/** Allocates a copy of a WPI_String */
124 if (str.str == nullptr || str.len == 0) {
125 return WPI_String{nullptr, 0};
126 }
127 return alloc_wpi_string(to_string_view(&str));
128}
129} // namespace wpi
130#endif
Definition ntcore_cpp.h:26
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.h:115
constexpr std::string_view to_string_view(const struct WPI_String *str)
Converts a WPI_String to a string_view.
Definition string.h:25
constexpr WPI_String make_string(std::string_view view)
Converts a string_view to a WPI_String.
Definition string.h:34
WPI_String copy_wpi_string(const WPI_String &str)
Allocates a copy of a WPI_String.
Definition string.h:123
char * WPI_AllocateString(struct WPI_String *wpiString, size_t length)
Allocates a WPI_String for the specified length.
struct WPI_String * WPI_AllocateStringArray(size_t length)
Allocates an array of WPI_Strings.
void WPI_InitStringWithLength(struct WPI_String *wpiString, const char *utf8String, size_t length)
Initializes a WPI_String from a UTF-8 string and length.
void WPI_InitString(struct WPI_String *wpiString, const char *utf8String)
Initializes a WPI_String from a null terminated UTF-8 string.
void WPI_FreeString(const struct WPI_String *wpiString)
Frees a WPI_String that was allocated with WPI_AllocateString()
void WPI_FreeStringArray(const struct WPI_String *wpiStringArray, size_t length)
Frees a WPI_String array returned by WPI_AllocateStringArray().
A const UTF8 string.
Definition string.h:15
const char * str
Contents.
Definition string.h:17
size_t len
Length.
Definition string.h:19