summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-05-28 23:54:52 +0200
committerTyge Løvset <[email protected]>2021-05-28 23:54:52 +0200
commit11b690935b817a825ae057455309d45c48dcae77 (patch)
treeb47fc1086b4553ce14472c479446a01209a82907
parent032b6d654a1125fe172ef225fdacb87ac3e1fdc5 (diff)
downloadSTC-modified-11b690935b817a825ae057455309d45c48dcae77.tar.gz
STC-modified-11b690935b817a825ae057455309d45c48dcae77.zip
Changed the csview front() and back() API. Added csview_npos constant.
-rw-r--r--docs/csview_api.md11
-rw-r--r--include/stc/csview.h6
2 files changed, 10 insertions, 7 deletions
diff --git a/docs/csview_api.md b/docs/csview_api.md
index b23ec9c2..34366671 100644
--- a/docs/csview_api.md
+++ b/docs/csview_api.md
@@ -7,9 +7,9 @@ element of the sequence at position zero. The implementation holds two members:
**csview** is an efficient replacent for `const char*`. It never allocates memory, and therefore need not be destructed.
Its lifetime is limited by the source string storage. It keeps the length of the string, and does not call *strlen()*
when passing it around. It is faster when using`csview` as convertion type (raw) than `const char*` in associative
-containers with cstr keys. E.g. prefer `using_cmap_svkey()` over `using_cmap_strkey()`.
+containers with cstr keys. `using_cmap_svkey()` may perform better than `using_cmap_strkey()`.
-Note that a **csview** may not be null-terminated, and should therefore be printed the following way:
+Note: a **csview** may ***not be null-terminated***, and must therefore be printed like:
`printf("%.*s", csview_ARG(sv))`.
See the c++ class [std::basic_string_view](https://en.cppreference.com/w/cpp/string/basic_string_view) for a functional
@@ -35,6 +35,9 @@ csview csview_lit(const char literal_only[]); // same as c
size_t csview_size(csview sv);
size_t csview_length(csview sv);
bool csview_empty(csview sv);
+char csview_front(csview sv);
+char csview_back(csview sv);
+
void csview_clear(csview* self);
csview csview_substr(csview sv, intptr_t pos, size_t n); // negative pos count from end
@@ -48,9 +51,6 @@ bool csview_contains(csview sv, csview needle);
bool csview_begins_with(csview sv, csview sub);
bool csview_ends_with(csview sv, csview sub);
-const char* csview_front(const csview* self);
-const char* csview_back(const csview* self);
-
csview_iter_t csview_begin(const csview* self);
csview_iter_t csview_end(const csview* self);
void csview_next(csview_iter_t* it);
@@ -91,6 +91,7 @@ uint64_t csview_hash_ref(const csview* x, size_t ignored);
| Name | Value | Usage |
|:-----------------|:--------------------|:----------------------------------|
| `csview_null` | same as `c_lit("")` | `sview = csview_null;` |
+| `csview_npos` | same as `cstr_npos` | |
| `c_lit(literal)` | csview constructor | `sview = c_lit("hello, world");` |
| `csview_ARG(sv)` | printf argument | `printf("%.*s", csview_ARG(sv));` |
diff --git a/include/stc/csview.h b/include/stc/csview.h
index 4a8f704b..4a9d1dff 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -29,6 +29,7 @@ typedef struct { const char* str; size_t size; } csview;
typedef struct { const char *ref; } csview_iter_t;
typedef char csview_value_t;
#define csview_null c_make(csview){"", 0}
+#define csview_npos cstr_npos
#define csview_ARG(sv) (int)(sv).size, (sv).str
#define c_lit(literal) csview_lit(literal)
@@ -45,9 +46,10 @@ STC_INLINE csview csview_from_s(cstr s)
STC_INLINE size_t csview_size(csview sv) { return sv.size; }
STC_INLINE size_t csview_length(csview sv) { return sv.size; }
STC_INLINE bool csview_empty(csview sv) { return sv.size == 0; }
+STC_INLINE char csview_front(csview sv) { return sv.str[0]; }
+STC_INLINE char csview_back(csview sv) { return sv.str[sv.size - 1]; }
+
STC_INLINE void csview_clear(csview* self) { *self = csview_null; }
-STC_INLINE const char* csview_front(const csview* self) { return self->str; }
-STC_INLINE const char* csview_back(const csview* self) { return self->str + self->size - 1; }
#define csview_hash(sv) c_default_hash((sv).str, (sv).size)
STC_INLINE bool csview_equals(csview sv, csview sv2)