WPILibC++ 2027.0.0-alpha-5
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#include <stddef.h> // NOLINT
8
9/**
10 * A const UTF8 string.
11 *
12 * Notes on usage:
13 * 1. WPILib will not have any APIs that manipulate an externally allocated
14 * string.
15 * 2. If a WPILib API takes a `const WPI_String*`, that string is an input.
16 * WPILib will not manipulate or attempt to free that string. It is up to
17 * the caller to manage the memory- WPILib will never hold onto that memory
18 * longer than the call.
19 * 3. If a WPILib API takes a `WPI_String*`, that string is an output. WPILib
20 * will allocate that string with WPI_AllocateString(), fill in the string,
21 * and return to the caller. When the caller is done with the string, they
22 * must free it with WPI_FreeString().
23 * 4. If an input struct containing a WPI_String, or an input array of
24 * [WPI_String](@ref WPI_String)s is passed to WPILib, the individual
25 * strings will not be manipulated or freed by WPILib, and the caller owns
26 * and should free that memory.
27 * 5. If an output struct contains a WPI_String member, that member is
28 * considered read only, and should not be explicitly freed individually.
29 * The caller should call the free function for that struct.
30 * 6. If an array of [WPI_String](@ref WPI_String)s is returned, each
31 * individual string is considered read only, and should not be explicitly
32 * freed individually. The caller should call the free function for that
33 * array.
34 * 7. Callbacks also follow these rules. The most common situation is a
35 * callback either getting passed a `const WPI_String*` or a struct
36 * containing a WPI_String. In both of these cases, the callback target
37 * should consider these strings read only, and not attempt to free them or
38 * manipulate them.
39 */
40struct WPI_String {
41 /** Contents. */
42 const char* str;
43 /** Length */
44 size_t len;
45};
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51/**
52 * Initializes a WPI_String from a null terminated UTF-8 string.
53 * If input string is null, initializes output to 0 length.
54 * The output length does not include the null terminator.
55 *
56 * The lifetime of the output string is the lifetime of the input string.
57 * Do not call WPI_FreeString() with the output of this call.
58 *
59 * @param wpiString output string
60 * @param utf8String input string (null terminated)
61 */
62void WPI_InitString(struct WPI_String* wpiString, const char* utf8String);
63
64/**
65 * Initializes a WPI_String from a UTF-8 string and length.
66 * If input string is null or 0 length, initializes output to 0 length.
67 * The input string does not need to be null terminated.
68 *
69 * The lifetime of the output string is the lifetime of the input string.
70 * Do not call WPI_FreeString() with the output of this call.
71 *
72 * @param wpiString output string
73 * @param utf8String input string
74 * @param length input string length in chars
75 */
76void WPI_InitStringWithLength(struct WPI_String* wpiString,
77 const char* utf8String, size_t length);
78
79/**
80 * Allocates a WPI_String for the specified length.
81 * The resultant string must be freed with WPI_FreeString().
82 *
83 * @param wpiString output string
84 * @param length string length in chars to allocate
85 * @return mutable pointer to allocated buffer
86 *
87 */
88char* WPI_AllocateString(struct WPI_String* wpiString, size_t length);
89
90/**
91 * Frees a WPI_String that was allocated with WPI_AllocateString()
92 *
93 * @param wpiString string to free
94 */
95void WPI_FreeString(const struct WPI_String* wpiString);
96
97/**
98 * Allocates an array of WPI_Strings.
99 *
100 * @param length array length
101 * @return string array
102 */
103struct WPI_String* WPI_AllocateStringArray(size_t length);
104
105/**
106 * Frees a WPI_String array returned by WPI_AllocateStringArray().
107 *
108 * @param wpiStringArray string array to free
109 * @param length length of array
110 */
111void WPI_FreeStringArray(const struct WPI_String* wpiStringArray,
112 size_t length);
113
114#ifdef __cplusplus
115} // extern "C"
116#endif
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:40
const char * str
Contents.
Definition string.h:42
size_t len
Length.
Definition string.h:44