Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
StringPath.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Internal/IGrowableBuffer.h"
5#include "../Foundation/StringSpan.h"
6
7namespace SC
8{
9
10namespace detail
11{
13template <int N>
14struct StringNativeFixed
15{
16 size_t length = 0;
17 native_char_t buffer[N];
18
19 [[nodiscard]] auto view() const { return StringSpan({buffer, length}, true, StringEncoding::Native); }
20 [[nodiscard]] bool isEmpty() const { return length == 0; }
21 [[nodiscard]] auto writableSpan() { return Span<native_char_t>{buffer, N}; }
22 [[nodiscard]] bool operator==(StringSpan other) const { return view() == other; }
23 [[nodiscard]] bool assign(StringSpan str)
24 {
25 length = 0;
26 return append(str);
27 }
28
29 [[nodiscard]] bool append(StringSpan str)
30 {
31 StringSpan::NativeWritable string = {{buffer, N}, length};
32 if (not str.appendNullTerminatedTo(string))
33 return false;
34 length = string.length;
35 return true;
36 }
37};
38} // namespace detail
39
41struct SC_COMPILER_EXPORT StringPath
42{
44#if SC_PLATFORM_WINDOWS
45 static constexpr size_t MaxPath = 260; // Equal to 'MAX_PATH' on Windows
46#elif SC_PLATFORM_APPLE
47 static constexpr size_t MaxPath = 1024; // Equal to 'PATH_MAX' on macOS
48#else
49 static constexpr size_t MaxPath = 4096; // Equal to 'PATH_MAX' on Linux
50#endif
51 [[nodiscard]] StringSpan view() const { return path.view(); }
52 [[nodiscard]] StringEncoding getEncoding() const { return StringEncoding::Native; }
53
54 [[nodiscard]] bool isEmpty() const { return path.view().isEmpty(); }
55 [[nodiscard]] bool append(StringSpan str) { return path.append(str); }
56 [[nodiscard]] bool assign(StringSpan str) { return path.assign(str); }
57 [[nodiscard]] bool resize(size_t newSize);
58
59 [[nodiscard]] Span<native_char_t> writableSpan() { return path.writableSpan(); }
60
61 private:
62 detail::StringNativeFixed<MaxPath> path;
63};
64
65template <>
66struct SC_COMPILER_EXPORT GrowableBuffer<StringPath> final : public IGrowableBuffer
67{
68 StringPath& sp;
69 GrowableBuffer(StringPath& string);
70 virtual ~GrowableBuffer() override;
71 virtual bool tryGrowTo(size_t newSize) override;
72};
73} // namespace SC
char native_char_t
The native char for the platform (wchar_t (4 bytes) on Windows, char (1 byte) everywhere else )
Definition PrimitiveTypes.h:34
Pre-sized char array holding enough space to represent a file system path.
Definition StringPath.h:42
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37
constexpr bool isEmpty() const
Return true if StringView is empty.
Definition StringSpan.h:85