@@ -13,6 +13,7 @@ with existing libraries, so decided to make the right one, once and forever.
13
13
* Light-weight references to strings: cheap to create, copy, or pass by value;
14
14
* Support for copy and move semantics, although not enforceable by the C language;
15
15
* String composition functions writing to memory, file descriptors, or file streams;
16
+ * Can be compiled using ` gcc ` or ` clang ` , and linked with ` libc ` or ` musl ` .
16
17
17
18
## Installation
18
19
Just clone the project and copy (or symlink) the files ` str.h ` and ` str.c ` into your project,
@@ -57,20 +58,20 @@ _**Disclaimer:** This is the good old C language, not C++ or Rust, so nothing ca
57
58
on the language level, and certain discipline is required to make sure there is no corrupt
58
59
or leaked memory resulting from using this library._
59
60
60
- A string is represented by the type ` str ` that maintains a pointer to some memory containing
61
- the actual string. Objects of type ` str ` are small enough (a struct of a ` const char* ` and a ` size_t ` )
62
- to be cheap to create, copy (pass by value), and move. The ` str ` structure should be treated
63
- as opaque (i.e., do not attempt to directly access or modify the fields in this structure).
64
- The strings are assumed to be immutable, like those in Java or Go, but only by means of ` const char* `
65
- pointers, so it is actually possible to write to such a string, although the required type
66
- cast to ` char* ` offers at least some (mostly psychological) protection from modifying a string
67
- by mistake.
61
+ A string is represented by the type ` str ` that maintains a pointer to some memory containing the
62
+ actual string, and the length of the string. Objects of type ` str ` are small enough (a struct
63
+ of a ` const char* ` and a ` size_t ` ) to be cheap to create, copy (pass by value), and move. The
64
+ ` str ` structure should be treated as opaque (i.e., do not attempt to directly access or modify
65
+ the fields in this structure). The strings are assumed to be immutable, like those in Java or
66
+ Go, but only by means of ` const char* ` pointers, so it is actually possible to modify such a
67
+ string, although the required type cast to ` char* ` offers at least some (mostly psychological)
68
+ protection from changing the string by mistake.
68
69
69
70
This library focusses only on handling strings, not gradually composing them like
70
71
[ StringBuffer] ( https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html )
71
72
class in Java.
72
73
73
- All string objects must be initialised. Uninitialised objects will cause
74
+ All string objects must be initialised before use . Uninitialised objects will cause
74
75
undefined behaviour. Use the provided constructors, or ` str_null ` for empty strings.
75
76
76
77
There are two kinds of ` str ` objects: those actually owning the memory they point to, and
@@ -98,7 +99,7 @@ can also be declared as `str_auto` (or `const str_auto`) for automatic cleanup w
98
99
goes out of scope.
99
100
* An owning object can be moved to another location by using ` str_move ` function. The
100
101
function resets its source object to an empty string.
101
- * An owning object can be passed over to another location by using ` str_pass ` function. The
102
+ * Object ownership can be passed over to another object by using ` str_pass ` function. The
102
103
function sets its source to a non-owning reference to the original string.
103
104
104
105
It is technically possible to create a reference to a string that is not
@@ -111,7 +112,7 @@ A string object can be constructed form any C string, string literal, or a range
111
112
The provided constructors are computationally cheap to apply. Depending on the constructor,
112
113
the new object can either own the actual string it refers to, or be a non-owning reference.
113
114
Constructors themselves do not allocate any memory. Importantly, constructors are the only
114
- functions in this library that return a string object, while others assign their results
115
+ functions in this library that return a string object, while others only assign their results
115
116
through a pointer to a pre-existing string. This makes constructors suitable for initialisation
116
117
of new string objects. In all other situations one should combine construction with assignment,
117
118
for example:<br >
@@ -167,11 +168,11 @@ str_join(&s, str_lit(", "),
167
168
str_lit("there"),
168
169
str_lit("and everywhere"));
169
170
170
- // create a new string concatenating "s" and a literal; the function does not modify its
171
- // destination object "s" before the result is computed, also freeing the destination
171
+ // create a new string concatenating "s" and a literal; the function only modifies its
172
+ // destination object "s" after the result is computed, also freeing the destination
172
173
// before the assignment, so it is safe to use "s" as both a parameter and a destination.
173
174
// note: we pass a copy of the owning object "s" as the second parameter, and here it is
174
- // safe to do so because this particular function does not store or release its arguments.
175
+ // safe to do so because this particular function does not modify its arguments.
175
176
str_cat(&s, s, str_lit("..."));
176
177
177
178
// check that we have got the expected result
0 commit comments