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