CTRE Phoenix 6 C++ 26.0.0-beta-1
Loading...
Searching...
No Matches
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 <span>
11#include <sstream>
12#include <stdint.h>
13#include <string>
14#include <vector>
15
16namespace ctre {
17namespace phoenix {
18namespace string_util {
19
20 /** Splits a string by the given delimiter */
21 CTREXPORT std::vector<std::string> string_split(std::string const &input, char delimiter);
22 /** Creates a new, lowercase version of the given string */
23 CTREXPORT std::string toLower(std::string_view input);
24 /** Converts the given string to lowercase in-place */
25 CTREXPORT void makeLower(std::string &input);
26 /** Creates a new, uppercase version of the given string */
27 CTREXPORT std::string toUpper(std::string_view input);
28 /** Converts the given string to uppercase in-place */
29 CTREXPORT void makeUpper(std::string &input);
30 /** Case-insensitive strcmp */
31 CTREXPORT int strcmp_nocase(char const *a, char const *b);
32 /** \returns number of doubles copied */
33 CTREXPORT uint32_t safe_copyDoubles(double *dest, std::span<double const> src, int32_t capacityDoubles);
34 /** Safely copies data from the source to the destination. At most capacity bytes will be copied. */
35 CTREXPORT size_t safe_memcpy(void *dest, void const *src, size_t numBytesToCopy, size_t capacity);
36 /**
37 * Safely copies a string to the destination. The source will be copied until the
38 * destination is full, and the destination will be null-terminated.
39 */
40 CTREXPORT char *safe_strcpy(char *dest, char const *src, size_t capacity);
41 /**
42 * Safely copies a string to the destination. The source will be copied until the
43 * destination is full or the source capacity has been reached, and the destination
44 * will be null-terminated.
45 */
46 CTREXPORT char *safe_strcpy(char *dest, char const *src, size_t capacityDest, size_t capacitySrc);
47 /**
48 * Safely concatenates a string to the destination. The source will be concatenated
49 * until the destination is full, and the destination will be null-terminated.
50 */
51 CTREXPORT char *safe_strcat(char *dest, char const *src, size_t capacity);
52 /**
53 * Safely concatenates a string to the destination. The source will be concatenated
54 * until the destination is full or the source capacity has been reached,
55 * and the destination will be null-terminated.
56 */
57 CTREXPORT char *safe_strcat(char *dest, char const *src, size_t capacityDest, size_t capacitySrc);
58 /**
59 * Safer strtok that finds and returns a pointer to the next token
60 * in *str_ptr, separated using one of the delimiters passed in.
61 * This also function modifies the input str_ptr to point to the
62 * remaining string after the delimiter, or NULL if none is found.
63 *
64 * This function is similar to the POSIX `strsep()`
65 *
66 * Unlike strtok, you should always pass in str_ptr.
67 * The caller must make sure both inputs are null terminated.
68 *
69 * ~~~{.c}
70 * char str_buffer[] = "\nThis is\n a test ";
71 * char *str = str_buffer;
72 * char *token = safe_strtok(&str, " \n");
73 * while (token != NULL) {
74 * printf("%s\n", token);
75 * token = safe_strtok(&str, " \n");
76 * }
77 * ~~~
78 */
79 CTREXPORT char *safe_strtok(char **str_ptr, char const *delimiters);
80
81 /** Safely determines the length of a string with the given capacity. */
82 CTREXPORT constexpr size_t safe_strlen(char const *s, size_t capacity)
83 {
84 if (!s) return 0;
85 size_t len = 0;
86 /* count the letters, if max or null terminator is reached leave */
87 for (; len < capacity && s[len]; ++len);
88 return len;
89 }
90
91 /**
92 * \brief Converts string to number while informing caller if operation was successful.
93 *
94 * \tparam T Type of number to convert to
95 * \param str String to convert from
96 * \param value Value of Type T to capture numeric value
97 * \returns true iff operation was successful
98 */
99 template <typename T>
100 bool toNumber(std::string const &str, T &value)
101 {
102 std::istringstream iss{str};
103 iss >> std::ws >> value >> std::ws;
104 return iss.eof();
105 }
106
107} // namespace string_util
108} // namespace phoenix
109} // namespace ctre
#define CTREXPORT
Definition export.h:14
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:82
bool toNumber(std::string const &str, T &value)
Converts string to number while informing caller if operation was successful.
Definition string_util.hpp:100
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.
CTREXPORT uint32_t safe_copyDoubles(double *dest, std::span< double const > src, int32_t capacityDoubles)
Definition motor_constants.h:14