F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Loading...
Searching...
No Matches
StringUtils.cpp
Go to the documentation of this file.
1#include "StringUtils.hpp"
2#include <Fw/Types/Assert.hpp>
3#include <cstring>
4#include <limits>
5
6char* Fw::StringUtils::string_copy(char* destination, const char* source, FwSizeType num) {
7 // Handle self-copy and 0 bytes copy
8 if (destination == source || num == 0) {
9 return destination;
10 }
11 FW_ASSERT(source != nullptr);
12 FW_ASSERT(destination != nullptr);
13
14 // Copying an overlapping range is undefined
15 FwSizeType source_len = string_length(source, num) + 1;
16 FW_ASSERT(source + source_len <= destination || destination + num <= source);
17
18 char* returned = strncpy(destination, source, num);
19 destination[num - 1] = '\0';
20 return returned;
21}
22
24 FwSizeType length = 0;
25 FW_ASSERT(source != nullptr);
26 for (length = 0; length < buffer_size; length++) {
27 if (source[length] == '\0') {
28 break;
29 }
30 }
31 return length;
32}
33
35 FwSizeType source_size,
36 const CHAR* sub_string,
37 FwSizeType sub_size) {
38 FW_ASSERT(source_string != nullptr);
39 FW_ASSERT(sub_string != nullptr);
40
41 // zero size sub-strings should always match
42 if ((source_size > 0) && (0 == sub_size)) {
43 return 0;
44 }
45
46 // Cannot find a substring larger than the source
47 if (source_size < sub_size) {
48 return -1;
49 }
50 // Confirm that the output type can hold the range of valid results
51 FW_ASSERT(static_cast<FwSignedSizeType>(source_size - sub_size) <= std::numeric_limits<FwSignedSizeType>::max());
52
53 // Loop from zero to source_size - sub_size (inclusive)
54 for (FwSizeType source_index = 0;
55 source_index < (source_size - sub_size + 1) &&
56 source_index < static_cast<FwSizeType>(std::numeric_limits<FwSignedSizeType>::max());
57 source_index++) {
58 // if the current character matches
59 for (FwSizeType sub_index = 0; sub_index < sub_size; sub_index++) {
60 // Prevent read overrun
61 FW_ASSERT((source_index + sub_index) < source_size);
62 // if there is a mismatch, go to next character
63 if (source_string[source_index + sub_index] != sub_string[sub_index]) {
64 break;
65 } else if (sub_index == (sub_size - 1)) {
66 // if we matched all the way to the end of the substring
67 return static_cast<FwSignedSizeType>(source_index);
68 }
69 }
70 }
71
72 // if we make it here, no matches were found
73 return -1;
74}
#define FW_ASSERT(...)
Definition Assert.hpp:14
char CHAR
Definition BasicTypes.h:32
PlatformSignedSizeType FwSignedSizeType
Definition FpConfig.h:30
PlatformSizeType FwSizeType
Definition FpConfig.h:35
FwSizeType string_length(const CHAR *source, FwSizeType buffer_size)
get the length of the source string
char * string_copy(char *destination, const char *source, FwSizeType num)
copy string with null-termination guaranteed
FwSignedSizeType substring_find(const CHAR *source_string, FwSizeType source_size, const CHAR *sub_string, FwSizeType sub_size)
find the first occurrence of a substring