Skip to content

Commit

Permalink
Cleanup usages of drop_front() and drop_back()
Browse files Browse the repository at this point in the history
  • Loading branch information
KangarooKoala committed Dec 30, 2023
1 parent 6c1d1fc commit 5ca5cea
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 34 deletions.
4 changes: 2 additions & 2 deletions glass/src/libnt/native/cpp/NTField2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void NTField2DModel::Update() {
for (auto&& event : m_poller.ReadQueue()) {
if (auto info = event.GetTopicInfo()) {
// handle publish/unpublish
auto name = wpi::drop_front(info->name, m_path.size());
auto name = wpi::remove_prefix(info->name, m_path);
if (name.empty() || name[0] == '.') {
continue;
}
Expand Down Expand Up @@ -198,7 +198,7 @@ void NTField2DModel::ForEachFieldObject(
func) {
for (auto&& obj : m_objects) {
if (obj->Exists()) {
func(*obj, wpi::drop_front(obj->GetName(), m_path.size()));
func(*obj, wpi::remove_prefix(obj->GetName(), m_path));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions glass/src/libnt/native/cpp/NTMechanism2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ NTMechanism2DModel::~NTMechanism2DModel() = default;
void NTMechanism2DModel::Update() {
for (auto&& event : m_poller.ReadQueue()) {
if (auto info = event.GetTopicInfo()) {
auto name = wpi::drop_front(info->name, m_path.size());
auto name = wpi::remove_prefix(info->name, m_path);
if (name.empty() || name[0] == '.') {
continue;
}
Expand Down Expand Up @@ -307,7 +307,7 @@ void NTMechanism2DModel::Update() {
}
} else {
auto fullName = nt::Topic{valueData->topic}.GetName();
auto name = wpi::drop_front(fullName, m_path.size());
auto name = wpi::remove_prefix(fullName, m_path);
if (name.empty() || name[0] == '.') {
continue;
}
Expand Down
37 changes: 21 additions & 16 deletions glass/src/libnt/native/cpp/NetworkTables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,10 +726,10 @@ void NetworkTablesModel::ValueSource::UpdateFromValue(
UpdateMsgpackValueSource(model, this, r, name, value.last_change());
mpack_reader_destroy(&r);
} else if (wpi::starts_with(typeStr, "struct:")) {
auto structName = wpi::drop_front(typeStr, 7);
bool isArray = structName.ends_with("[]");
auto structName = wpi::remove_prefix(typeStr, "struct:");
bool isArray = wpi::ends_with(structName, "[]");
if (isArray) {
structName = wpi::drop_back(structName, 2);
structName = wpi::remove_suffix(structName, "[]");
}
auto desc = model.m_structDb.Find(structName);
if (desc && desc->IsValid()) {
Expand Down Expand Up @@ -761,7 +761,7 @@ void NetworkTablesModel::ValueSource::UpdateFromValue(
valueChildren.clear();
}
} else if (wpi::starts_with(typeStr, "proto:")) {
auto msg = model.m_protoDb.Find(wpi::drop_front(typeStr, 6));
auto msg = model.m_protoDb.Find(wpi::remove_prefix(typeStr, "proto:"));
if (msg) {
msg->Clear();
auto raw = value.GetRaw();
Expand Down Expand Up @@ -807,12 +807,14 @@ void NetworkTablesModel::Update() {
} else if (info->name == "$serversub") {
m_server.subscribers.clear();
} else if (wpi::starts_with(info->name, "$clientpub$")) {
auto it = m_clients.find(wpi::drop_front(info->name, 11));
auto it =
m_clients.find(wpi::remove_prefix(info->name, "$clientpub$"));
if (it != m_clients.end()) {
it->second.publishers.clear();
}
} else if (wpi::starts_with(info->name, "$clientsub$")) {
auto it = m_clients.find(wpi::drop_front(info->name, 11));
auto it =
m_clients.find(wpi::remove_prefix(info->name, "$clientsub$"));
if (it != m_clients.end()) {
it->second.subscribers.clear();
}
Expand Down Expand Up @@ -853,12 +855,14 @@ void NetworkTablesModel::Update() {
} else if (entry->info.name == "$serversub") {
m_server.UpdateSubscribers(entry->value.GetRaw());
} else if (wpi::starts_with(entry->info.name, "$clientpub$")) {
auto it = m_clients.find(wpi::drop_front(entry->info.name, 11));
auto it = m_clients.find(
wpi::remove_prefix(entry->info.name, "$clientpub$"));
if (it != m_clients.end()) {
it->second.UpdatePublishers(entry->value.GetRaw());
}
} else if (wpi::starts_with(entry->info.name, "$clientsub$")) {
auto it = m_clients.find(wpi::drop_front(entry->info.name, 11));
auto it = m_clients.find(
wpi::remove_prefix(entry->info.name, "$clientsub$"));
if (it != m_clients.end()) {
it->second.UpdateSubscribers(entry->value.GetRaw());
}
Expand All @@ -867,7 +871,8 @@ void NetworkTablesModel::Update() {
wpi::starts_with(entry->info.name, "/.schema/struct:") &&
entry->info.type_str == "structschema") {
// struct schema handling
auto typeStr = wpi::drop_front(entry->info.name, 16);
auto typeStr =
wpi::remove_prefix(entry->info.name, "/.schema/struct:");
std::string_view schema{
reinterpret_cast<const char*>(entry->value.GetRaw().data()),
entry->value.GetRaw().size()};
Expand All @@ -886,9 +891,8 @@ void NetworkTablesModel::Update() {
if (!wpi::starts_with(ts, "struct:")) {
continue;
}
ts = wpi::drop_front(ts, 7);
if (ts == typeStr || (wpi::ends_with(ts, "[]") &&
wpi::drop_back(ts, 2) == typeStr)) {
ts = wpi::remove_prefix(ts, "struct:");
if (wpi::remove_suffix(ts, "[]") == typeStr) {
entryPair.second->UpdateFromValue(*this);
}
}
Expand All @@ -897,7 +901,8 @@ void NetworkTablesModel::Update() {
wpi::starts_with(entry->info.name, "/.schema/proto:") &&
entry->info.type_str == "proto:FileDescriptorProto") {
// protobuf descriptor handling
auto filename = wpi::drop_front(entry->info.name, 15);
auto filename =
wpi::remove_prefix(entry->info.name, "/.schema/proto:");
if (!m_protoDb.Add(filename, entry->value.GetRaw())) {
fmt::print("could not decode protobuf '{}' filename '{}'\n",
entry->info.name, filename);
Expand Down Expand Up @@ -1079,17 +1084,17 @@ void NetworkTablesModel::UpdateClients(std::span<const uint8_t> data) {

static bool GetHeadingTypeString(std::string_view* ts) {
if (wpi::starts_with(*ts, "proto:")) {
*ts = wpi::drop_front(*ts, 6);
*ts = wpi::remove_prefix(*ts, "proto:");
auto lastdot = ts->rfind('.');
if (lastdot != std::string_view::npos) {
*ts = wpi::substr(*ts, lastdot + 1);
}
if (wpi::starts_with(*ts, "Protobuf")) {
*ts = wpi::drop_front(*ts, 8);
*ts = wpi::remove_prefix(*ts, "Protobuf");
}
return true;
} else if (wpi::starts_with(*ts, "struct:")) {
*ts = wpi::drop_front(*ts, 7);
*ts = wpi::remove_prefix(*ts, "struct:");
return true;
}
return false;
Expand Down
5 changes: 3 additions & 2 deletions glass/src/libnt/native/cpp/NetworkTablesProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void NetworkTablesProvider::Update() {
}

auto topicName = nt::GetTopicName(valueData->topic);
auto tableName = wpi::drop_back(topicName, 6);
auto tableName = wpi::remove_prefix(topicName, "/.type");

GetOrCreateView(builderIt->second, nt::Topic{valueData->topic},
tableName);
Expand Down Expand Up @@ -186,7 +186,8 @@ void NetworkTablesProvider::Show(ViewEntry* entry, Window* window) {
}
if (wpi::starts_with(entry->name, "/SmartDashboard/")) {
window->SetDefaultName(
fmt::format("{} (SmartDashboard)", wpi::drop_front(entry->name, 16)));
fmt::format("{} (SmartDashboard)",
wpi::remove_prefix(entry->name, "/SmartDashboard/")));
}
entry->window = window;

Expand Down
8 changes: 4 additions & 4 deletions ntcore/src/main/native/cpp/LocalStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ bool LocalStorage::MultiSubscriberData::Matches(std::string_view name,
}

int LocalStorage::DataLoggerData::Start(TopicData* topic, int64_t time) {
return log.Start(fmt::format("{}{}", logPrefix,
wpi::drop_front(topic->name, prefix.size())),
topic->typeStr == "int" ? "int64" : topic->typeStr,
DataLoggerEntry::MakeMetadata(topic->propertiesStr), time);
return log.Start(
fmt::format("{}{}", logPrefix, wpi::remove_prefix(topic->name, prefix)),
topic->typeStr == "int" ? "int64" : topic->typeStr,
DataLoggerEntry::MakeMetadata(topic->propertiesStr), time);
}

void LocalStorage::DataLoggerEntry::Append(const Value& v) {
Expand Down
2 changes: 1 addition & 1 deletion ntcore/src/main/native/cpp/NetworkServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ void NetworkServer::ServerConnection4::ProcessWsUpgrade() {
std::string_view name;
bool err = false;
if (wpi::starts_with(path, "/nt/")) {
name = wpi::UnescapeURI(wpi::drop_front(path, 4), nameBuf, &err);
name = wpi::UnescapeURI(wpi::remove_prefix(path, "/nt/"), nameBuf, &err);
}
if (err || name.empty()) {
INFO("invalid path '{}' (from {}), must match /nt/[clientId], closing",
Expand Down
3 changes: 1 addition & 2 deletions ntcore/src/main/native/cpp/net/WireDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,6 @@ bool nt::net::WireDecodeBinary(std::span<const uint8_t>* in, int64_t* outId,
outValue->SetServerTime(time);
outValue->SetTime(time == 0 ? 0 : time + localTimeOffset);
// update input range
*in = wpi::drop_front(*in,
in->size() - mpack_reader_remaining(&reader, nullptr));
*in = wpi::take_back(*in, mpack_reader_remaining(&reader, nullptr));
return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ void HALSimHttpConnection::ProcessRequest() {
// convert to fs native representation
fs::path nativePath;
if (wpi::starts_with(path, "/user/")) {
nativePath =
fs::path{m_server->GetWebrootSys()} /
fs::path{wpi::drop_front(path, 6), fs::path::format::generic_format};
nativePath = fs::path{m_server->GetWebrootSys()} /
fs::path{wpi::remove_prefix(path, "/user/"),
fs::path::format::generic_format};
} else {
nativePath =
fs::path{m_server->GetWebrootSys()} /
fs::path{wpi::drop_front(path, 1), fs::path::format::generic_format};
fs::path{wpi::drop_front(path), fs::path::format::generic_format};
}

if (fs::is_directory(nativePath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ bool wpi::detail::ConsumeSignedInteger(
}

// Get the positive part of the value.
std::string_view str2 = wpi::drop_front(str, 1);
std::string_view str2 = wpi::drop_front(str);
if (wpi::detail::ConsumeUnsignedInteger(str2, radix, ullVal) ||
// Reject values so large they'd overflow as negative signed, but allow
// "-0". This negates the unsigned so that the negative isn't undefined
Expand Down

0 comments on commit 5ca5cea

Please sign in to comment.