Compare commits

..

2 commits

Author SHA1 Message Date
fef
5aed426864
string: add more utility functions 2021-07-15 01:47:04 +02:00
fef
43da1220ee
error: fix brainfart 2021-07-14 22:58:49 +02:00
12 changed files with 474 additions and 10 deletions

View file

@ -13,6 +13,7 @@ extern "C" {
#include "neo/_error.h"
#include "neo/_types.h"
#include "neo/_stddef.h"
#include "neo/_string.h"
#include "neo/_nalloc.h"
#ifdef __cplusplus

View file

@ -66,7 +66,7 @@ void errput(error *err);
* Get an optional error message, this may be `nil`
* Must only be used within a catch block and before `errput` is called.
*/
#define errmsg(err) ((err) == nil ? nil : (err)->_message)
#define errmsg(err) ((err) == nil ? (string *)nil : (err)->_message)
#ifdef __cplusplus
}; /* extern "C" */

View file

@ -21,6 +21,13 @@ extern "C" {
# define offsetof(type, member) __builtin_offsetof(type, member)
#endif
/** Get the absolute (non negative) value of an integer */
#define nabs(n) ({ \
/* n is an expression, not a variable, evaluate it only once */ \
typeof(n) __neo_local_n = (n); \
__neo_local_n < 0 ? -__neo_local_n : __neo_local_n; \
})
/**
* Declare a length field in a structure.
* This makes it compatible with the `nlen` macro.

View file

@ -7,6 +7,7 @@ extern "C" {
#endif
#include "neo/_types.h"
#include "neo/_toolchain.h"
/**
* Copy a regular C string to a neo string.
@ -19,7 +20,7 @@ extern "C" {
* @param err: Error pointer
* @returns The converted string, unless an error occurred
*/
string *nstr(const char *restrict s, error *err);
string *nstr(const char *__restrict s, error *err);
/**
* Get the Unicode code point in a string at the specified index.
@ -32,7 +33,136 @@ string *nstr(const char *restrict s, error *err);
* @returns The Unicode code point at the specified index,
* unless an error occurred
*/
nchar nchrat(string *s, usize index, error *err);
nchar nchrat(const string *s, usize index, error *err);
/**
* Convert a signed integer to a string.
*
* If allocation fails or the radix is out of range, an error is yeeted.
*
* @param i: The integer
* @param radix: Base of the numerical system to convert to, must be within 2~36
* @param err: Error pointer
* @returns The stringified integer, unless an error occurred
*/
string *i2nstr(i64 i, int radix, error *err);
/**
* Convert an unsigned integer to a string.
*
* If allocation fails or the radix is out of range, an error is yeeted.
*
* @param u: The integer
* @param radix: Base of the numerical system to convert to, must be within 2~36
* @param err: Error pointer
* @returns The stringified integer, unless an error occurred
*/
string *u2nstr(u64 u, int radix, error *err);
/**
* Duplicate a string.
*
* If `s` is `nil` or allocation fails, an error is yeeted.
*
* @param s: String to duplicate
* @param err: Error pointer
* @returns The duplicated string, unless an error occurred
*/
string *nstrdup(const string *s, error *err);
/**
* Repeat a string `n` times and return the new string.
*
* If `s` is `nil` or allocation fails, an error is yeeted.
* If `n` is 0, an empty string is returned.
* If `n` is 1, this function behaves exactly like `nstrdup`.
*
* @param s: String to repeat, remains unmodified
* @param n: Total amount of repetitions
* @param err: Error pointer
* @returns A new string with the repeated content, unless an error occurred
*/
string *nstrmul(const string *s, usize n, error *err);
/**
* Create a string considting of `n` repetitions of `c`.
*
* If `c` is not within unicode space or allocation fails, an error is yeeted.
* If `n` is 0, an empty string is returned.
*
* @param c: Character to fill the string with
* @param n: How many characters to put into the string
* @param err: Error pointer
* @returns The string, unless an error occurred
*/
string *nchrmul(nchar c, usize n, error *err);
/**
* Concatenate two strings and return the result.
*
* The original two strings are unmodified.
* If any of the two strings are `nil` or allocation fails, an error is yeeted.
*
* @param s1: First string
* @param s2: Second string
* @param err: Error pointer
* @returns A new string instance that consists of the two concatenated ones,
* unless an error occurred
*/
string *nstrcat(const string *s1, const string *s2, error *err);
/**
* Compare two strings character by character.
* The return value is negative if the first different character in `s1` comes
* before the one in `s2`
*
* @param s1: First string
* @param s2: Second string
* @param err: Error pointer
* @returns The difference between the two strings, unless an error occurred
*/
int nstrcmp(const string *s1, const string *s2, error *err);
/**
* Determine whether two strings are exactly equal.
* If either of the two string are `nil` or acquiring a lock on them fails,
* an error is yeeted.
*
* @param s1: First `string *`
* @param s2: Second `string *`
* @param err: Error pointer
* @returns Whether the two strings are equal, unless an error occurred
*/
#define nstreq(s1, s2, err) ( (bool)(nstrcmp(s1, s2, err) != 0) )
/**
* Prepend fill characters to a string to make it a specific length, and return
* a new string with the result. The original string is unmodified.
* If the string is already longer than the desired width or allocation fails,
* an error is yeeted.
*
* @param s: String to expand
* @param length: Desired length of the new string
* @param fill: Character to fill the space with
* @param err: Error pointer
* @returns The new padded string
*/
string *leftpad(const string *s, usize length, nchar fill, error *err);
/**
* Iterate over each character in a string.
*
* @param nstr: `string *` to iterate over
* @param nchr: `nchar *` to store the current character in
* @param err: Error pointer; the loop will terminate early if an uncaught
* error occurred
*/
#define nstr_foreach_nchr(nstr, nchr, err) \
for (const char *__pos = \
(nstr)->_data + utf8_to_nchr(nchr, (nstr)->_data, err); \
*__pos != '\0' && (err) != nil && \
(err)->_number + 1 < 2; \
__pos += utf8_to_nchr(nchr, __pos, err))
#ifdef __cplusplus
}; /* extern "C" */

View file

@ -26,8 +26,8 @@ void yeet(error *err, u32 number, const char *restrict fmt, ...)
va_start(vargs, fmt);
vsnprintf_ret = vsnprintf(msg, msg_capacity, fmt, vargs);
va_end(vargs);
if (vsnprintf_ret > 0) {
msg_capacity += vsnprintf_ret;
if (vsnprintf_ret > msg_capacity) {
msg_capacity = vsnprintf_ret;
nfree(msg);
msg = nil;
} else if (vsnprintf_ret < 0) {

78
src/string/leftpad.c Normal file
View file

@ -0,0 +1,78 @@
/** See the end of this file for copyright and license terms. */
#include <errno.h>
#include <string.h>
#include "neo/_error.h"
#include "neo/_nalloc.h"
#include "neo/_nref.h"
#include "neo/_string.h"
#include "neo/_types.h"
#include "neo/utf.h"
static inline string *leftpad_unsafe(const string *s, usize len, nchar fillchr, error *err)
{
char utf8_fillchr[5];
usize fillchr_size = utf8_from_nchr(&utf8_fillchr[0], fillchr, err);
catch(err) {
return 0;
}
usize extra_chars = len - s->_len;
usize size_now = s->_capacity + 1;
usize size_after = size_now + (extra_chars * fillchr_size);
char *dest = nalloc(size_after, err);
catch(err) {
return 0;
}
char *pos = dest;
while (extra_chars-- > 0) {
for (int i = 0; i < fillchr_size; i++)
*pos++ = utf8_fillchr[i];
}
strcpy(pos, s->_data);
string *padded = nstr(dest, err);
catch(err) {
padded = nil;
}
nfree(dest);
return padded;
}
string *leftpad(const string *s, usize len, nchar fillchr, error *err)
{
if (s == nil) {
yeet(err, EFAULT, "String must not be nil");
return 0;
}
nget(s);
string *padded;
if (len < s->_len) {
yeet(err, ERANGE, "String is longer than requested length");
padded = nil;
} else if (s->_len == len) {
padded = nstrdup(s, err);
} else {
padded = leftpad_unsafe(s, len, fillchr, err);
}
nput(s);
return padded;
}
/*
* This file is part of libneo.
* Copyright (c) 2021 Fefie <owo@fef.moe>.
*
* libneo is non-violent software: you may only use, redistribute,
* and/or modify it under the terms of the CNPLv6+ as found in
* the LICENSE file in the source code root directory or at
* <https://git.pixie.town/thufie/CNPL>.
*
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/

View file

@ -58,15 +58,13 @@ string *nstr(const char *restrict s, error *err)
return str;
}
nchar nchrat(string *s, usize index, error *err)
nchar nchrat(const string *s, usize index, error *err)
{
if (s == nil) {
yeet(err, EFAULT, "String is nil");
return '\0';
}
nget(s);
if (index >= s->_len) {
yeet(err, ERANGE, "String index out of bounds");
return '\0';
@ -80,8 +78,6 @@ nchar nchrat(string *s, usize index, error *err)
nchar ret;
utf8_to_nchr(&ret, ptr, err);
nput(s);
return ret;
}

50
src/string/nstrcat.c Normal file
View file

@ -0,0 +1,50 @@
/** See the end of this file for copyright and license terms. */
#include <errno.h>
#include <string.h>
#include "neo/_error.h"
#include "neo/_nalloc.h"
#include "neo/_nref.h"
#include "neo/_stddef.h"
#include "neo/_string.h"
#include "neo/_types.h"
string *nstrcat(const string *s1, const string *s2, error *err)
{
if (s1 == nil) {
yeet(err, EFAULT, "First string is nil");
return nil;
}
if (s2 == nil) {
yeet(err, EFAULT, "Second string is nil");
return nil;
}
usize s1_size = strlen(s1->_data);
usize s2_size = strlen(s2->_data);
char *cat = nalloc(s1_size + s2_size + 1, err);
catch(err) {
return nil;
}
strcpy(cat, s1->_data);
strcpy(cat + s1_size, s2->_data);
string *ret = nstr(cat, err);
nfree(cat);
return ret;
}
/*
* This file is part of libneo.
* Copyright (c) 2021 Fefie <owo@fef.moe>.
*
* libneo is non-violent software: you may only use, redistribute,
* and/or modify it under the terms of the CNPLv6+ as found in
* the LICENSE file in the source code root directory or at
* <https://git.pixie.town/thufie/CNPL>.
*
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/

47
src/string/nstrcmp.c Normal file
View file

@ -0,0 +1,47 @@
/** See the end of this file for copyright and license terms. */
#include <errno.h>
#include <string.h>
#include "neo/_error.h"
#include "neo/_nref.h"
#include "neo/_stddef.h"
#include "neo/_string.h"
#include "neo/_types.h"
int nstrcmp(const string *s1, const string *s2, error *err)
{
if (s1 == nil) {
yeet(err, EFAULT, "First string is nil");
return 0;
}
if (s2 == nil) {
yeet(err, EFAULT, "Second string is nil");
return 0;
}
int ret;
if (nlen(s1) > nlen(s2))
ret = 1;
else if (nlen(s1) < nlen(s2))
ret = -1;
else
ret = strcmp(s1->_data, s2->_data);
neat(err);
return ret;
}
/*
* This file is part of libneo.
* Copyright (c) 2021 Fefie <owo@fef.moe>.
*
* libneo is non-violent software: you may only use, redistribute,
* and/or modify it under the terms of the CNPLv6+ as found in
* the LICENSE file in the source code root directory or at
* <https://git.pixie.town/thufie/CNPL>.
*
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/

80
src/string/nstrmul.c Normal file
View file

@ -0,0 +1,80 @@
/** See the end of this file for copyright and license terms. */
#include <errno.h>
#include <string.h>
#include "neo/_error.h"
#include "neo/_nalloc.h"
#include "neo/_nref.h"
#include "neo/_stddef.h"
#include "neo/_string.h"
#include "neo/_types.h"
#include "neo/utf.h"
string *nstrmul(const string *s, usize n, error *err)
{
if (s == nil) {
yeet(err, EFAULT, "String is nil");
return 0;
}
if (nlen(s) == 0 || n == 0)
return nstr("", err);
if (n == 1)
return nstrdup(s, err);
usize s_size = strlen(s->_data);
char *multiplied = nalloc(s_size * n + 1, err);
catch(err) {
return nil;
}
char *pos = multiplied;
while (n-- != 0) {
strcpy(pos, s->_data);
pos += s_size;
}
string *ret = nstr(multiplied, err);
nfree(multiplied);
return ret;
}
string *nchrmul(nchar c, usize n, error *err)
{
if (n == 0)
return nstr("", err);
char s[5];
usize s_size = utf8_from_nchr(&s[0], c, err);
catch(err) {
return nil;
}
char *multiplied = nalloc(s_size * n + 1, err);
catch(err) {
return nil;
}
char *pos = multiplied;
while (n-- != 0) {
strncpy(pos, &s[0], s_size);
pos += s_size;
}
string *ret = nstr(multiplied, err);
nfree(multiplied);
return ret;
}
/*
* This file is part of libneo.
* Copyright (c) 2021 Fefie <owo@fef.moe>.
*
* libneo is non-violent software: you may only use, redistribute,
* and/or modify it under the terms of the CNPLv6+ as found in
* the LICENSE file in the source code root directory or at
* <https://git.pixie.town/thufie/CNPL>.
*
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/

View file

@ -1,4 +1,9 @@
target_sources(neo PRIVATE
./string/nstr.c
./string/nstrcat.c
./string/nstrcmp.c
./string/nstrmul.c
./string/leftpad.c
./string/utf.c
./string/x2nstr.c
)

70
src/string/x2nstr.c Normal file
View file

@ -0,0 +1,70 @@
/** See the end of this file for copyright and license terms. */
#include <errno.h>
#include "neo/_error.h"
#include "neo/_string.h"
#include "neo/_types.h"
static char *unsigned_convert(char *end, u64 n, int radix, error *err)
{
static const char digits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
};
if (radix < 2 || radix > (sizeof(digits) / sizeof(digits[0]))) {
yeet(err, EINVAL, "Numerical base out of range");
return nil;
}
*end-- = '\0';
do {
unsigned int digit = n % radix;
n /= radix;
*end-- = digits[digit];
} while (n != 0);
end++;
neat(err);
return end;
}
string *u2nstr(u64 n, int radix, error *err)
{
char buf[65];
char *s = unsigned_convert(&buf[64], n, radix, err);
catch (err) {
return nil;
}
return nstr(s, err);
}
string *i2nstr(i64 n, int radix, error *err)
{
char buf[66];
char *s = unsigned_convert(&buf[65], nabs(n), radix, err);
catch(err) {
return nil;
}
if (n < 0)
*(--s) = '-';
return nstr(s, err);
}
/*
* This file is part of libneo.
* Copyright (c) 2021 Fefie <owo@fef.moe>.
*
* libneo is non-violent software: you may only use, redistribute,
* and/or modify it under the terms of the CNPLv6+ as found in
* the LICENSE file in the source code root directory or at
* <https://git.pixie.town/thufie/CNPL>.
*
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/