nasa / fprime

F´ - A flight software and embedded systems framework
https://fprime.jpl.nasa.gov
Apache License 2.0
9.95k stars 1.28k forks source link

Work/string search #2720

Closed timcanham closed 1 month ago

timcanham commented 2 months ago
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.

Joshua-Anderson commented 1 month ago

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 commented 1 month ago

@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 commented 1 month ago

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.