StrInitFromCstr
StrInitFromCstr
Description
Initializes a Str object from a C-style string (cstr) with a specified length (len). This macro creates a new Str object and copies up to len characters from cstr.
Parameters
| Name | Direction | Description |
|---|---|---|
cstr |
in | Pointer to the null-terminated C-style string to initialize from. |
len |
in | The number of characters to copy from cstr. |
Success
Returns a newly created Str object with its data field pointing to a newly allocated memory containing a copy of the first len characters of cstr. The length and capacity fields are set to len. copy_init and copy_deinit are set to NULL, and alignment is set to 1.
Failure
Returns a Str object with data set to NULL if memory allocation using ZstrDupN fails. In such a case, length and capacity will likely be uninitialized or zero. It’s crucial to check the data field for NULL after using this macro to handle potential memory allocation errors.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Str.c:24:
// Create Str from input data
Str str = StrInitFromCstr((const char *)(data + *offset), len);
*offset += len;- In
Str.c:52:
if (cstr) {
StrDeinit(str);
*str = StrInitFromCstr(cstr, strlen(cstr));
free(cstr);
}- In
Io.c:1869:
// Create a temporary Str for parsing
Str temp = StrInitFromCstr(start, i - start);
// Try to parse as special value
- In
Io.c:1927:
// Create a temporary Str for parsing
Str temp = StrInitFromCstr(start, pos);
// Validate the string is a proper floating point number
- In
Io.c:1982:
// Create a temporary Str for parsing
Str temp = StrInitFromCstr(start, pos);
// Check for special prefixes with no digits
- In
Io.c:2057:
// Create a temporary Str for parsing
Str temp = StrInitFromCstr(start, pos);
// Check for special prefixes with no digits
- In
Io.c:2131:
// Create a temporary Str for parsing
Str temp = StrInitFromCstr(start, pos);
// Check for special prefixes with no digits
- In
Io.c:2206:
// Create a temporary Str for parsing
Str temp = StrInitFromCstr(start, pos);
// Check for special prefixes with no digits
- In
Io.c:2271:
// Create a temporary Str for parsing
Str temp = StrInitFromCstr(start, pos);
// Check for special prefixes with no digits
- In
Io.c:2346:
// Create a temporary Str for parsing
Str temp = StrInitFromCstr(start, pos);
// Check for special prefixes with no digits
- In
Io.c:2421:
// Create a temporary Str for parsing
Str temp = StrInitFromCstr(start, pos);
// Check for special prefixes with no digits
- In
Io.c:2496:
// Create a temporary Str for parsing
Str temp = StrInitFromCstr(start, pos);
// Check for special prefixes with no digits
- In
Io.c:2698:
// Parse hex string
Str hex_str = StrInitFromCstr(hex_start, i - hex_start);
u64 value;
StrParseConfig config = {.base = 16};- In
Io.c:2734:
// Parse octal string
Str oct_str = StrInitFromCstr(oct_start, i - oct_start);
u64 value;
StrParseConfig config = {.base = 8};- In
Io.c:2767:
// Create string from binary digits (already null-terminated by StrInitFromCstr)
Str bin_str = StrInitFromCstr(bin_start, i - bin_start);
// Convert to BitVec using the null-terminated string
- In
Io.c:2833:
}
Str temp = StrInitFromCstr(start, i - start);
Int parsed = IntFromStrRadix(temp.data, radix);- In
Io.c:2877:
}
temp = StrInitFromCstr(start, token_len);
parsed = FloatFromStr(temp.data);- In
Io.c:2924:
// Create a temporary Str for parsing
Str temp = StrInitFromCstr(start, i - start);
// Try to parse as special value
- In
Io.c:2984:
// Create a temporary Str for parsing
Str temp = StrInitFromCstr(start, pos);
// Validate the string is a proper floating point number
- In
Str.c:131:
const char *next = ZstrFindSubstring(prev, key);
if (next) {
Str tmp = StrInitFromCstr(prev, next - prev);
VecPushBack(&sv, tmp); // exclude delimiter
prev = next + keylen; // skip past delimiter
- In
Str.c:136:
} else {
if (ZstrCompareN(prev, key, end - prev)) {
Str tmp = StrInitFromCstr(prev, end - prev);
VecPushBack(&sv, tmp); // remaining part
}- In
Str.c:183:
size new_len = end >= start ? (end - start + 1) : 0;
return StrInitFromCstr(start, new_len);
}- In
Dir.c:175:
direntry.type = SYS_DIR_ENTRY_TYPE_UNKNOWN;
}
direntry.name = StrInitFromCstr(entry->d_name, NAMELEN(entry));
VecPushBack(&dc, direntry);
}- In
Str.Init.c:40:
// Test StrInitFromCstr function
bool test_str_init_from_cstr(void) {
WriteFmt("Testing StrInitFromCstr\n");
const char *test_str = "Hello, World!";- In
Str.Init.c:44:
const char *test_str = "Hello, World!";
size_t len = 5; // Just "Hello"
Str s = StrInitFromCstr(test_str, len);
// Validate the string
- In
Init.h:19:
#ifdef __cplusplus
# define StrInitFromCstr(cstr, len) \
(Str { \
.data = ZstrDupN((char *)(cstr), (len)), \- In
Init.h:75:
/// potential memory allocation errors.
///
#define StrInitFromZstr(zstr) StrInitFromCstr((zstr), strlen(zstr))
///
- In
Init.h:80:
/// Initialize a Str object using another one
///
#define StrInitFromStr(str) StrInitFromCstr((str)->data, (str)->length)
#define StrDup(str) StrInitFromStr(str)
Last updated on