F´ Flight Software - C/C++ Documentation devel
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StringUtils.cpp
Go to the documentation of this file.
1#include "StringUtils.hpp"
2#include <Fw/Types/Assert.hpp>
3#include <cstring>
4
5char* Fw::StringUtils::string_copy(char* destination, const char* source, U32 num) {
6 // Handle self-copy and 0 bytes copy
7 if(destination == source || num == 0) {
8 return destination;
9 }
10
11 // Copying an overlapping range is undefined
12 U32 source_len = string_length(source, num) + 1;
13 FW_ASSERT(source + source_len <= destination || destination + num <= source);
14
15 char* returned = strncpy(destination, source, num);
16 destination[num - 1] = '\0';
17 return returned;
18}
19
20U32 Fw::StringUtils::string_length(const CHAR* source, U32 max_len) {
21 U32 length = 0;
22 FW_ASSERT(source != nullptr);
23 for (length = 0; length < max_len; length++) {
24 if (source[length] == '\0') {
25 break;
26 }
27 }
28 return length;
29}
#define FW_ASSERT(...)
Definition Assert.hpp:14
char CHAR
Definition BasicTypes.h:28
char * string_copy(char *destination, const char *source, U32 num)
copy string with null-termination guaranteed
U32 string_length(const CHAR *source, U32 max_len)
get the length of the source string or max_len if the string is longer than max_len.