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