StringView.hxx 3.46 KB
Newer Older
1
/*
2
 * Copyright (C) 2013-2015 Max Kellermann <max.kellermann@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef STRING_VIEW_HXX
#define STRING_VIEW_HXX

#include "ConstBuffer.hxx"

#include <string.h>

struct StringView : ConstBuffer<char> {
	StringView() = default;

	constexpr StringView(pointer_type _data, size_type _size)
		:ConstBuffer<char>(_data, _size) {}

	constexpr StringView(pointer_type _begin, pointer_type _end)
		:ConstBuffer<char>(_begin, _end - _begin) {}

	StringView(pointer_type _data)
		:ConstBuffer<char>(_data,
				   _data != nullptr ? strlen(_data) : 0) {}

50
	constexpr StringView(std::nullptr_t n)
51 52 53 54 55 56
		:ConstBuffer<char>(n) {}

	static constexpr StringView Empty() {
		return StringView("", size_t(0));
	}

57 58 59 60 61 62 63 64 65 66
	template<size_t n>
	static constexpr StringView Literal(const char (&_data)[n]) {
		static_assert(n > 0, "");
		return {_data, n - 1};
	}

	static constexpr StringView Literal() {
		return StringView("", size_t(0));
	}

67
	void SetEmpty() noexcept {
68 69 70 71 72
		data = "";
		size = 0;
	}

	gcc_pure
73
	pointer_type Find(char ch) const noexcept {
74 75 76
		return (pointer_type)memchr(data, ch, size);
	}

77
	StringView &operator=(std::nullptr_t) noexcept {
78 79 80 81 82
		data = nullptr;
		size = 0;
		return *this;
	}

83
	StringView &operator=(pointer_type _data) noexcept {
84 85 86 87 88 89
		data = _data;
		size = _data != nullptr ? strlen(_data) : 0;
		return *this;
	}

	gcc_pure
90
	bool StartsWith(StringView needle) const noexcept {
91 92 93 94 95
		return size >= needle.size &&
			memcmp(data, needle.data, needle.size) == 0;
	}

	gcc_pure
96
	bool Equals(StringView other) const noexcept {
97 98 99 100 101
		return size == other.size &&
			memcmp(data, other.data, size) == 0;
	}

	template<size_t n>
102
	bool EqualsLiteral(const char (&other)[n]) const noexcept {
103
		return Equals(Literal(other));
104 105 106
	}

	gcc_pure
107
	bool EqualsIgnoreCase(StringView other) const noexcept {
108 109 110 111 112
		return size == other.size &&
			strncasecmp(data, other.data, size) == 0;
	}

	template<size_t n>
113
	bool EqualsLiteralIgnoreCase(const char (&other)[n]) const noexcept {
114
		return EqualsIgnoreCase(Literal(other));
115
	}
116 117 118 119

	/**
	 * Skip all whitespace at the beginning.
	 */
120
	void StripLeft() noexcept;
121 122 123 124

	/**
	 * Skip all whitespace at the end.
	 */
125
	void StripRight() noexcept;
126

127
	void Strip() noexcept {
128 129 130
		StripLeft();
		StripRight();
	}
131 132 133
};

#endif