summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--NEWS2
-rw-r--r--doc/overview_code.c5
-rw-r--r--doc/string_views.rst13
-rw-r--r--include/zix/string_view.h11
-rw-r--r--meson.build2
-rw-r--r--src/path.c6
6 files changed, 31 insertions, 8 deletions
diff --git a/NEWS b/NEWS
index b619e92..8ea9471 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,4 @@
-zix (0.3.1) unstable; urgency=medium
+zix (0.3.3) unstable; urgency=medium
* Initial release
diff --git a/doc/overview_code.c b/doc/overview_code.c
index 1370292..2f6a099 100644
--- a/doc/overview_code.c
+++ b/doc/overview_code.c
@@ -1,4 +1,4 @@
-// Copyright 2021-2022 David Robillard <d@drobilla.net>
+// Copyright 2021-2023 David Robillard <d@drobilla.net>
// SPDX-License-Identifier: ISC
/*
@@ -26,8 +26,9 @@ string_views(void)
// end make-empty-string
// begin make-static-string
- ZixStringView hello = zix_string("hello");
+ static const ZixStringView hello = ZIX_STATIC_STRING("hello");
// end make-static-string
+ (void)hello;
// begin measure-string
ZixStringView view = zix_string(string_pointer);
diff --git a/doc/string_views.rst b/doc/string_views.rst
index b6e3fd9..6c049f3 100644
--- a/doc/string_views.rst
+++ b/doc/string_views.rst
@@ -1,5 +1,5 @@
..
- Copyright 2020-2022 David Robillard <d@drobilla.net>
+ Copyright 2020-2023 David Robillard <d@drobilla.net>
SPDX-License-Identifier: ISC
String Views
@@ -15,6 +15,17 @@ This forces code to be explicit about string measurement,
which discourages common patterns of repeated measurement of the same string.
For convenience, several macros and functions are provided for constructing string views:
+:macro:`ZIX_STATIC_STRING`
+
+ Initializes a string view from a string literal. Note that this may only be
+ used with inlined string literals, passing a pointer to an arbitrary string
+ is undefined behaviour, for example:
+
+ .. literalinclude:: overview_code.c
+ :start-after: begin make-static-string
+ :end-before: end make-static-string
+ :dedent: 2
+
:func:`zix_empty_string`
Constructs a view of an empty string, for example:
diff --git a/include/zix/string_view.h b/include/zix/string_view.h
index f1fe009..f13f8d1 100644
--- a/include/zix/string_view.h
+++ b/include/zix/string_view.h
@@ -1,4 +1,4 @@
-// Copyright 2011-2021 David Robillard <d@drobilla.net>
+// Copyright 2011-2023 David Robillard <d@drobilla.net>
// SPDX-License-Identifier: ISC
#ifndef ZIX_STRING_VIEW_H
@@ -29,6 +29,15 @@ typedef struct {
size_t length; ///< Length of string in bytes
} ZixStringView;
+/// Initialize a string view from a string literal
+// clang-format off
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
+# define ZIX_STATIC_STRING(s) (ZixStringView) {s, (sizeof(s) - 1U)}
+#else
+# define ZIX_STATIC_STRING(s) { s, (sizeof(s) - 1U) }
+#endif
+// clang-format on
+
/// Return a view of an empty string
ZIX_ALWAYS_INLINE_FUNC
ZIX_CONST_FUNC
diff --git a/meson.build b/meson.build
index fc687a0..1ca5ec4 100644
--- a/meson.build
+++ b/meson.build
@@ -12,7 +12,7 @@ project(
],
license: 'ISC',
meson_version: '>= 0.56.0',
- version: '0.3.1',
+ version: '0.3.3',
)
zix_src_root = meson.current_source_dir()
diff --git a/src/path.c b/src/path.c
index 385e6a8..8df04e6 100644
--- a/src/path.c
+++ b/src/path.c
@@ -496,6 +496,8 @@ zix_path_lexically_relative(ZixAllocator* const allocator,
const char* const path,
const char* const base)
{
+ static const ZixStringView dot = ZIX_STATIC_STRING(".");
+
// Mismatched roots can't be expressed in relative form
const ZixRootSlices path_root = zix_path_root_slices(path);
const ZixRootSlices base_root = zix_path_root_slices(base);
@@ -520,7 +522,7 @@ zix_path_lexically_relative(ZixAllocator* const allocator,
// Matching paths reduce to "."
if ((a.state == ZIX_PATH_END && b.state == ZIX_PATH_END) ||
(zix_is_empty_range(a.range) && b.state == ZIX_PATH_END)) {
- return zix_string_view_copy(allocator, zix_string("."));
+ return zix_string_view_copy(allocator, dot);
}
// Count the trailing non-matching entries in base
@@ -546,7 +548,7 @@ zix_path_lexically_relative(ZixAllocator* const allocator,
// A result with no up-references or names reduces to "."
if (n_up == 0 && a.state == ZIX_PATH_END) {
- return zix_string_view_copy(allocator, zix_string("."));
+ return zix_string_view_copy(allocator, dot);
}
// Allocate buffer for relative path result