CTRE Phoenix 6 C++ 24.3.0
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 <sstream>
11#include <stdint.h>
12#include <string>
13#include <vector>
14
15namespace ctre {
16namespace phoenix {
17namespace string_util {
18
19 /** Splits a string by the given delimiter */
20 CTREXPORT std::vector<std::string> string_split(std::string const &input, char delimiter);
21 /** Creates a new, lowercase version of the given string */
22 CTREXPORT std::string toLower(std::string_view input);
23 /** Converts the given string to lowercase in-place */
24 CTREXPORT void makeLower(std::string &input);
25 /** Creates a new, uppercase version of the given string */
26 CTREXPORT std::string toUpper(std::string_view input);
27 /** Converts the given string to uppercase in-place */
28 CTREXPORT void makeUpper(std::string &input);
29 /** Case-insensitive strcmp */
30 CTREXPORT int strcmp_nocase(char const *a, char const *b);
31 /** \returns number of doubles copied */
32 CTREXPORT uint32_t safe_copyDoubles(double *dest, double const *src, int32_t numDoubles, int32_t capacityDoubles);
33 /** \returns number of doubles copied */
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 /**
93 * \brief Converts string to number while informing caller if operation was successful.
94 *
95 * \tparam T Type of number to convert to
96 * \param str String to convert from
97 * \param value Value of Type T to capture numeric value
98 * \returns true iff operation was successful
99 */
100 template <typename T>
101 bool toNumber(std::string const &str, T &value)
102 {
103 std::istringstream iss{str};
104 iss >> std::ws >> value >> std::ws;
105 return iss.eof();
106 }
107
108} // namespace string_util
109} // namespace phoenix
110} // 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
bool toNumber(std::string const &str, T &value)
Converts string to number while informing caller if operation was successful.
Definition: string_util.hpp:101
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:15