string: add more utility functions
parent
43da1220ee
commit
5aed426864
@ -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.
|
||||
*/
|
@ -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.
|
||||
*/
|
@ -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.
|
||||
*/
|
@ -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.
|
||||
*/
|
@ -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
|
||||
)
|
||||
|
@ -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.
|
||||
*/
|
Loading…
Reference in New Issue