F´ Flight Software - C/C++ Documentation  NASA-v1.6.0
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 
5 char* 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 
20 U32 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 }
StringUtils.hpp
CHAR
char CHAR
Definition: BasicTypes.hpp:99
Fw::StringUtils::string_copy
char * string_copy(char *destination, const char *source, U32 num)
copy string with null-termination guaranteed
Definition: StringUtils.cpp:5
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
Fw::StringUtils::string_length
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.
Definition: StringUtils.cpp:20