CTRE Phoenix Pro C++ 23.0.12
string_util.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
10#include <stdint.h>
11#include <string>
12#include <vector>
13
14namespace ctre {
15namespace phoenix {
16namespace string_util {
17
18 /** Splits a string by the given delimiter */
19 CTREXPORT std::vector<std::string> string_split(std::string const &input, char delimiter);
20 /** Creates a new, lowercase version of the given string */
21 CTREXPORT std::string toLower(std::string_view input);
22 /** Converts the given string to lowercase in-place */
23 CTREXPORT void makeLower(std::string &input);
24 /** Creates a new, uppercase version of the given string */
25 CTREXPORT std::string toUpper(std::string_view input);
26 /** Converts the given string to uppercase in-place */
27 CTREXPORT void makeUpper(std::string &input);
28 /** Case-insensitive strcmp */
29 CTREXPORT int strcmp_nocase(char const *a, char const *b);
30 /**
31 * \returns number of doubles copied
32 */
33 CTREXPORT uint32_t safe_copyDoubles(double *dest, double const *src, int32_t numDoubles, int32_t capacityDoubles);
34 CTREXPORT uint32_t safe_copyDoubles(double *dest, std::vector<double> const &src, int32_t capacityDoubles);
35 /** Safely copies data from the source to the destination. At most capacity bytes will be copied. */
36 CTREXPORT size_t safe_memcpy(void *dest, void const *src, size_t numBytesToCopy, size_t capacity);
37 /**
38 * Safely copies a string to the destination. The source will be copied until the
39 * destination is full, and the destination will be null-terminated.
40 */
41 CTREXPORT char *safe_strcpy(char *dest, char const *src, size_t capacity);
42 /**
43 * Safely copies a string to the destination. The source will be copied until the
44 * destination is full or the source capacity has been reached, and the destination
45 * will be null-terminated.
46 */
47 CTREXPORT char *safe_strcpy(char *dest, char const *src, size_t capacityDest, size_t capacitySrc);
48 /**
49 * Safely concatenates a string to the destination. The source will be concatenated
50 * until the destination is full, and the destination will be null-terminated.
51 */
52 CTREXPORT char *safe_strcat(char *dest, char const *src, size_t capacity);
53 /**
54 * Safely concatenates a string to the destination. The source will be concatenated
55 * until the destination is full or the source capacity has been reached,
56 * and the destination will be null-terminated.
57 */
58 CTREXPORT char *safe_strcat(char *dest, char const *src, size_t capacityDest, size_t capacitySrc);
59 /**
60 * Safer strtok that finds and returns a pointer to the next token
61 * in *str_ptr, separated using one of the delimiters passed in.
62 * This also function modifies the input str_ptr to point to the
63 * remaining string after the delimiter, or NULL if none is found.
64 *
65 * This function is similar to the POSIX `strsep()`
66 *
67 * Unlike strtok, you should always pass in str_ptr.
68 * The caller must make sure both inputs are null terminated.
69 *
70 * ~~~{.c}
71 * char str_buffer[] = "\nThis is\n a test ";
72 * char *str = str_buffer;
73 * char *token = safe_strtok(&str, " \n");
74 * while (token != NULL) {
75 * printf("%s\n", token);
76 * token = safe_strtok(&str, " \n");
77 * }
78 * ~~~
79 */
80 CTREXPORT char *safe_strtok(char **str_ptr, char const *delimiters);
81
82 /** Safely determines the length of a string with the given capacity. */
83 CTREXPORT constexpr size_t safe_strlen(char const *s, size_t capacity)
84 {
85 if (!s) return 0;
86 size_t len = 0;
87 /* count the letters, if max or null terminator is reached leave */
88 for (; len < capacity && s[len]; ++len);
89 return len;
90 }
91
92} // namespace string_util
93} // namespace phoenix
94} // namespace ctre
#define CTREXPORT
Definition: export.h:14
CTREXPORT uint32_t safe_copyDoubles(double *dest, double const *src, int32_t numDoubles, int32_t capacityDoubles)
CTREXPORT char * safe_strcat(char *dest, char const *src, size_t capacity)
Safely concatenates a string to the destination.
CTREXPORT char * safe_strtok(char **str_ptr, char const *delimiters)
Safer strtok that finds and returns a pointer to the next token in *str_ptr, separated using one of t...
CTREXPORT size_t safe_memcpy(void *dest, void const *src, size_t numBytesToCopy, size_t capacity)
Safely copies data from the source to the destination.
CTREXPORT void makeLower(std::string &input)
Converts the given string to lowercase in-place.
CTREXPORT constexpr size_t safe_strlen(char const *s, size_t capacity)
Safely determines the length of a string with the given capacity.
Definition: string_util.hpp:83
CTREXPORT std::string toUpper(std::string_view input)
Creates a new, uppercase version of the given string.
CTREXPORT char * safe_strcpy(char *dest, char const *src, size_t capacity)
Safely copies a string to the destination.
CTREXPORT std::vector< std::string > string_split(std::string const &input, char delimiter)
Splits a string by the given delimiter.
CTREXPORT int strcmp_nocase(char const *a, char const *b)
Case-insensitive strcmp.
CTREXPORT std::string toLower(std::string_view input)
Creates a new, lowercase version of the given string.
CTREXPORT void makeUpper(std::string &input)
Converts the given string to uppercase in-place.
Definition: string_util.hpp:14