ofen/src/of_util.cpp

38 lines
1.1 KiB
C++
Raw Normal View History

2024-12-11 10:21:57 +08:00
#include "of_util.h"
namespace ofen {
void CMutBuffer::push(const char* data, int len)
{
std::lock_guard<std::mutex> lock(mutex_);
buffer_.insert(buffer_.end(), data, data + len);
}
int CMutBuffer::index_of(const char* data, int len, int start_pos)
{
std::lock_guard<std::mutex> lock(mutex_);
if (start_pos < 0 || start_pos >= static_cast<int>(buffer_.size()) || len <= 0) {
return -1;
}
auto it = std::search(buffer_.begin() + start_pos, buffer_.end(), data, data + len);
if (it != buffer_.end()) {
return std::distance(buffer_.begin(), it);
}
return -1;
}
void CMutBuffer::remove_of(int start_pos, int len)
{
std::lock_guard<std::mutex> lock(mutex_);
if (start_pos < 0 || start_pos >= static_cast<int>(buffer_.size()) || len <= 0) {
return;
}
auto end_pos = std::min(start_pos + len, static_cast<int>(buffer_.size()));
buffer_.erase(buffer_.begin() + start_pos, buffer_.begin() + end_pos);
}
void CMutBuffer::clear()
{
std::lock_guard<std::mutex> lock(mutex_);
buffer_.clear();
}
} // namespace ofen