Commit 07d2bc68 authored by Max Kellermann's avatar Max Kellermann

util/StringView: add method SplitLast()

parent 9551166f
/*
* Copyright 2013-2019 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2013-2020 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
......@@ -35,6 +35,7 @@
#include "Compiler.h"
#include <utility>
#include <cstddef>
#if __cplusplus >= 201703L && !GCC_OLDER_THAN(7,0)
#include <string_view>
......@@ -126,6 +127,20 @@ struct BasicStringView : ConstBuffer<T> {
return {{begin(), separator}, {separator + 1, end()}};
}
/**
* Split the string at the last occurrence of the given
* character. If the character is not found, then the first
* value is the whole string and the second value is nullptr.
*/
gcc_pure
std::pair<BasicStringView<T>, BasicStringView<T>> SplitLast(value_type ch) const noexcept {
const auto separator = FindLast(ch);
if (separator == nullptr)
return {*this, nullptr};
return {{begin(), separator}, {separator + 1, end()}};
}
gcc_pure
bool StartsWith(BasicStringView<T> needle) const noexcept {
return this->size >= needle.size &&
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment