Skip to content

Commit

Permalink
AP_Common: add method to find the offset of a byte in a buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbarker committed Feb 26, 2024
1 parent f673617 commit 9799832
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions libraries/AP_Common/AP_Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,13 @@ int16_t char_to_hex(char a)
else
return a - '0';
}

int16_t offset_of_byte_in_buffer(uint8_t byte, const uint8_t *buffer, uint16_t buffer_len)
{
const uint8_t *p = (const uint8_t *)memchr(buffer, byte, buffer_len);
if (p == nullptr) {
// preamble not found
return -1;
}
return p - buffer;
}
3 changes: 3 additions & 0 deletions libraries/AP_Common/AP_Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ size_t strncpy_noterm(char *dest, const char *src, size_t n);
// return the numeric value of an ascii hex character
int16_t char_to_hex(char a);

// returns offset into buffer where byte is found, -1 if not found:
int16_t offset_of_byte_in_buffer(uint8_t byte, const uint8_t *buffer, uint16_t buffer_len);

/*
Bit manipulation
*/
Expand Down

0 comments on commit 9799832

Please sign in to comment.