Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work/string search #2720

Merged
merged 9 commits into from
May 23, 2024
Merged

Work/string search #2720

merged 9 commits into from
May 23, 2024

Conversation

timcanham
Copy link
Collaborator

Related Issue(s) None
Has Unit Tests (y/n) y
Documentation Included (y/n) y, headers

Change Description

Added a function to string_utils to search for a substring in a string

Rationale

Centralizes a feature that could be useful to many people.

Testing/Review Recommendations

Future Work

Calling this function will be added to StringBase as a helper.

@timcanham timcanham self-assigned this May 8, 2024
Fw/Types/StringUtils.cpp Fixed Show resolved Hide resolved
Fw/Types/StringUtils.cpp Fixed Show fixed Hide fixed
Fw/Types/StringUtils.cpp Fixed Show fixed Hide fixed
Copy link
Collaborator

@bocchino bocchino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just a couple of comments.

Should we add a wrapper function in Fw::StringBase to call this function?

Fw/Types/StringUtils.cpp Outdated Show resolved Hide resolved
Fw/Types/StringUtils.cpp Outdated Show resolved Hide resolved
Fw/Types/StringUtils.cpp Outdated Show resolved Hide resolved
Fw/Types/StringUtils.cpp Fixed Show fixed Hide fixed
Fw/Types/StringUtils.cpp Fixed Show fixed Hide fixed
Fw/Types/StringUtils.cpp Fixed Show fixed Hide fixed
Fw/Types/StringUtils.cpp Fixed Show fixed Hide fixed
Fw/Types/StringUtils.cpp Fixed Show fixed Hide fixed
Fw/Types/StringUtils.cpp Fixed Show fixed Hide fixed
Fw/Types/StringUtils.cpp Fixed Show fixed Hide fixed
Fw/Types/StringUtils.cpp Fixed Show fixed Hide fixed
@Joshua-Anderson
Copy link
Collaborator

Joshua-Anderson commented May 9, 2024

Did you consider using the libc strstr function? It does have a prereq on the strings being null terminated (rather than providing a length argument) but if the plan is to use it with StringBase I think that may be reasonable assumption.

@timcanham
Copy link
Collaborator Author

@Joshua-Anderson I thought about it, but we prefer to use the "n" versions of string functions so they don't run off into space if the null terminator is corrupted. This is a home-grown version with an "n" adaptation, and the code is pretty small too.

@bocchino
Copy link
Collaborator

bocchino commented May 9, 2024

I thought about it, but we prefer to use the "n" versions of string functions so they don't run off into space if the null terminator is corrupted.

I second this. We should avoid using any string library functions that lack a buffer-bound argument. It looks like there is a strnstr function, but it is BSD-specific, so it won't work on general POSIX.

@timcanham timcanham marked this pull request as ready for review May 22, 2024 20:58
Copy link
Collaborator

@LeStarch LeStarch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor changes

Fw/Types/StringUtils.cpp Show resolved Hide resolved
Fw/Types/StringUtils.cpp Outdated Show resolved Hide resolved
Fw/Types/StringUtils.cpp Show resolved Hide resolved
Fw/Types/StringUtils.cpp Outdated Show resolved Hide resolved
Fw/Types/StringUtils.cpp Show resolved Hide resolved
Fw/Types/StringUtils.cpp Fixed Show fixed Hide fixed
Fw/Types/StringUtils.cpp Fixed Show fixed Hide fixed
Fw/Types/StringUtils.cpp Dismissed Show dismissed Hide dismissed
Fw/Types/StringUtils.cpp Dismissed Show dismissed Hide dismissed
FW_ASSERT((source_size - sub_size) <= std::numeric_limits<FwSignedSizeType>::max());

// Loop from zero to source_size - sub_size (inclusive)
for (FwSizeType source_index = 0;

Check notice

Code scanning / CodeQL

Use of basic integral type Note

source_index uses the basic integral type unsigned long rather than a typedef with size and signedness.
@@ -29,3 +30,45 @@
}
return length;
}

FwSignedSizeType Fw::StringUtils::substring_find(const CHAR* source_string,

Check notice

Code scanning / CodeQL

Use of basic integral type Note

substring_find uses the basic integral type signed long rather than a typedef with size and signedness.
@@ -29,3 +30,45 @@
}
return length;
}

FwSignedSizeType Fw::StringUtils::substring_find(const CHAR* source_string,

Check notice

Code scanning / CodeQL

Use of basic integral type Note

source_string uses the basic integral type char rather than a typedef with size and signedness.
@@ -29,3 +30,45 @@
}
return length;
}

FwSignedSizeType Fw::StringUtils::substring_find(const CHAR* source_string,
FwSizeType source_size,

Check notice

Code scanning / CodeQL

Use of basic integral type Note

source_size uses the basic integral type unsigned long rather than a typedef with size and signedness.

FwSignedSizeType Fw::StringUtils::substring_find(const CHAR* source_string,
FwSizeType source_size,
const CHAR* sub_string,

Check notice

Code scanning / CodeQL

Use of basic integral type Note

sub_string uses the basic integral type char rather than a typedef with size and signedness.
FwSignedSizeType Fw::StringUtils::substring_find(const CHAR* source_string,
FwSizeType source_size,
const CHAR* sub_string,
FwSizeType sub_size) {

Check notice

Code scanning / CodeQL

Use of basic integral type Note

sub_size uses the basic integral type unsigned long rather than a typedef with size and signedness.
@LeStarch LeStarch merged commit f4c2095 into nasa:devel May 23, 2024
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants