Loading [MathJax]/extensions/tex2jax.js
CTRE Phoenix 6 C++ 23.10.0-alpha-8
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 /**
32 * \returns number of doubles copied
33 */
34 CTREXPORT uint32_t safe_copyDoubles(double *dest, double const *src, int32_t numDoubles, int32_t capacityDoubles);
35 CTREXPORT uint32_t safe_copyDoubles(double *dest, std::vector<double> const &src, int32_t capacityDoubles);
36 /** Safely copies data from the source to the destination. At most capacity bytes will be copied. */
37 CTREXPORT size_t safe_memcpy(void *dest, void const *src, size_t numBytesToCopy, size_t capacity);
38 /**
39 * Safely copies a string to the destination. The source will be copied until the
40 * destination is full, and the destination will be null-terminated.
41 */
42 CTREXPORT char *safe_strcpy(char *dest, char const *src, size_t capacity);
43 /**
44 * Safely copies a string to the destination. The source will be copied until the
45 * destination is full or the source capacity has been reached, and the destination
46 * will be null-terminated.
47 */
48 CTREXPORT char *safe_strcpy(char *dest, char const *src, size_t capacityDest, size_t capacitySrc);
49 /**
50 * Safely concatenates a string to the destination. The source will be concatenated
51 * until the destination is full, and the destination will be null-terminated.
52 */
53 CTREXPORT char *safe_strcat(char *dest, char const *src, size_t capacity);
54 /**
55 * Safely concatenates a string to the destination. The source will be concatenated
56 * until the destination is full or the source capacity has been reached,
57 * and the destination will be null-terminated.
58 */
59 CTREXPORT char *safe_strcat(char *dest, char const *src, size_t capacityDest, size_t capacitySrc);
60 /**
61 * Safer strtok that finds and returns a pointer to the next token
62 * in *str_ptr, separated using one of the delimiters passed in.
63 * This also function modifies the input str_ptr to point to the
64 * remaining string after the delimiter, or NULL if none is found.
65 *
66 * This function is similar to the POSIX `strsep()`
67 *
68 * Unlike strtok, you should always pass in str_ptr.
69 * The caller must make sure both inputs are null terminated.
70 *
71 * ~~~{.c}
72 * char str_buffer[] = "\nThis is\n a test ";
73 * char *str = str_buffer;
74 * char *token = safe_strtok(&str, " \n");
75 * while (token != NULL) {
76 * printf("%s\n", token);
77 * token = safe_strtok(&str, " \n");
78 * }
79 * ~~~
80 */
81 CTREXPORT char *safe_strtok(char **str_ptr, char const *delimiters);
82
83 /** Safely determines the length of a string with the given capacity. */
84 CTREXPORT constexpr size_t safe_strlen(char const *s, size_t capacity)
85 {
86 if (!s) return 0;
87 size_t len = 0;
88 /* count the letters, if max or null terminator is reached leave */
89 for (; len < capacity && s[len]; ++len);
90 return len;
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 * @return 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:84
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: ManualEvent.hpp:12