From 803a54d93c78630929352d01a2ea7fcce0c5e590 Mon Sep 17 00:00:00 2001 From: Ignacio Etcheverry Date: Fri, 5 May 2017 02:09:43 +0200 Subject: [PATCH] RingBuffer: Adds find() method --- core/ring_buffer.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/core/ring_buffer.h b/core/ring_buffer.h index d5085e95604..8b32bb5e105 100644 --- a/core/ring_buffer.h +++ b/core/ring_buffer.h @@ -101,6 +101,32 @@ public: return p_size; }; + int find(const T &t, int p_offset, int p_max_size) { + + int left = data_left(); + if ((p_offset + p_max_size) > left) { + p_max_size -= left - p_offset; + if (p_max_size <= 0) + return 0; + } + p_max_size = MIN(left, p_max_size); + int pos = read_pos; + inc(pos, p_offset); + int to_read = p_max_size; + while (to_read) { + int end = pos + to_read; + end = MIN(end, size()); + int total = end - pos; + for (int i = 0; i < total; i++) { + if (data[pos + i] == t) + return i + (p_max_size - to_read); + }; + to_read -= total; + pos = 0; + } + return -1; + } + inline int advance_read(int p_n) { p_n = MIN(p_n, data_left()); inc(read_pos, p_n);