You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.2 KiB
C

/* See the end of this file for copyright and license terms. */
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include "neo/_error.h"
#include "neo/_nalloc.h"
#include "neo/_nref.h"
#include "neo/_nstr.h"
#include "neo/_stddef.h"
#include "neo/_types.h"
nstr_t *nstrtrim(const nstr_t *s, error *err)
{
if (s == nil) {
yeet(err, EFAULT, "String is nil");
return nil;
}
const char *start = s->_data;
const char *end = start + s->_size - 5; /* 4 NUL terminators */
while (isspace(*start++));
/* nothing */
start--;
while (isspace(*end) && end > start)
end--;
end++;
return nnstr(start, end - start, err);
}
nstr_t *nstrtrim_put(nstr_t *s, error *err)
{
nstr_t *ret = nstrtrim(s, err);
catch(err) {
return nil;
}
nput(s);
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.
*/