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/StringSpan.h"
5
6namespace SC
7{
8
9namespace detail
10{
12template <int N>
13struct StringNativeFixed
14{
15 size_t length = 0;
16 native_char_t buffer[N];
17
18 [[nodiscard]] StringSpan view() const { return StringSpan({buffer, length}, true, StringEncoding::Native); }
19
20 [[nodiscard]] Span<native_char_t> writableSpan() { return {buffer, N}; }
21
22 [[nodiscard]] bool assign(StringSpan str)
23 {
24 length = 0;
25 return append(str);
26 }
27
28 [[nodiscard]] bool append(StringSpan str)
29 {
30 StringSpan::NativeWritable string = {{buffer, N}, length};
31 if (not str.appendNullTerminatedTo(string))
32 return false;
33 length = string.length;
34 return true;
35 }
36};
37} // namespace detail
38
40struct SC_COMPILER_EXPORT StringPath
41{
43#if SC_PLATFORM_WINDOWS
44 static constexpr size_t MaxPath = 260; // Equal to 'MAX_PATH' on Windows
45#elif SC_PLATFORM_APPLE
46 static constexpr size_t MaxPath = 1024; // Equal to 'PATH_MAX' on macOS
47#else
48 static constexpr size_t MaxPath = 4096; // Equal to 'PATH_MAX' on Linux
49#endif
50 [[nodiscard]] StringSpan view() const { return path.view(); }
51
52 [[nodiscard]] bool append(StringSpan str) { return path.append(str); }
53 [[nodiscard]] bool assign(StringSpan str) { return path.assign(str); }
54 [[nodiscard]] bool resize(size_t newSize)
55 {
56 if (newSize < MaxPath)
57 {
58 path.length = newSize;
59 }
60 return newSize < MaxPath;
61 }
62
63 [[nodiscard]] Span<native_char_t> writableSpan() { return path.writableSpan(); }
64
65 private:
66 detail::StringNativeFixed<MaxPath> path;
67};
68} // 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:41
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37