Added strip command

Strips trailing whitespace from string
This commit is contained in:
AlexApps99 2020-01-22 23:14:11 +13:00 committed by Brenden Matthews
parent 703e9a6add
commit b8d3515f19
4 changed files with 28 additions and 0 deletions

View File

@ -3870,6 +3870,16 @@
<listitem>Converts all letters into uppercase.
<para /></listitem>
</varlistentry>
<varlistentry>
<term>
<command>
<option>strip</option>
</command>
<option>text</option>
</term>
<listitem>Strips all whitespace from ends of input.
<para /></listitem>
</varlistentry>
<varlistentry>
<term>
<command>

View File

@ -895,6 +895,9 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
END OBJ(uppercase, 0) obj->data.s = STRNDUP_ARG;
obj->callbacks.print = &print_uppercase;
obj->callbacks.free = &gen_free_opaque;
END OBJ(strip, 0) obj->data.s = STRNDUP_ARG;
obj->callbacks.print = &strip_trailing_whitespace;
obj->callbacks.free = &gen_free_opaque;
END OBJ(catp, 0) obj->data.s = STRNDUP_ARG;
obj->callbacks.print = &print_catp;
obj->callbacks.free = &gen_free_opaque;

View File

@ -117,6 +117,20 @@ void print_uppercase(struct text_object *obj, char *p,
}
}
void strip_trailing_whitespace(struct text_object *obj, char *p,
unsigned int p_max_size) {
evaluate(obj->data.s, p, p_max_size);
for (unsigned int x = p_max_size - 2;; x--) {
if (p[x] && !isspace(p[x])) {
p[x + 1] = '\0';
break;
} else if (x == 0) {
p[x] = '\0';
break;
}
}
}
long long int apply_base_multiplier(const char *s, long long int num) {
long long int base = 1024LL;
if (*s && (0 == (strcmp(s, "si")))) { base = 1000LL; }

View File

@ -38,5 +38,6 @@ void print_catp(struct text_object *, char *, unsigned int);
void print_startcase(struct text_object *, char *, unsigned int);
void print_lowercase(struct text_object *, char *, unsigned int);
void print_uppercase(struct text_object *, char *, unsigned int);
void strip_trailing_whitespace(struct text_object *, char *, unsigned int);
long long apply_base_multiplier(const char *, long long int);
#endif /* _MISC_H */