X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Fstr.c;h=afe32de9f2049bfcc5a80ac7a95b36b348be4cb7;hb=8830c95bb9e8d72621787866141a27fc22e8c786;hp=44d325e072df297eb80977d1f03abd45af6bd543;hpb=5c3291dc396b795696e94f47780308fd7ace6fc4;p=pspp-builds.git diff --git a/src/libpspp/str.c b/src/libpspp/str.c index 44d325e0..afe32de9 100644 --- a/src/libpspp/str.c +++ b/src/libpspp/str.c @@ -1071,6 +1071,34 @@ ds_set_length (struct string *st, size_t new_length, char pad) st->ss.length = new_length; } +/* Removes N characters from ST starting at offset START. */ +void +ds_remove (struct string *st, size_t start, size_t n) +{ + if (n > 0 && start < st->ss.length) + { + if (st->ss.length - start <= n) + { + /* All characters at or beyond START are deleted. */ + st->ss.length = start; + } + else + { + /* Some characters remain and must be shifted into + position. */ + memmove (st->ss.string + st->ss.length, + st->ss.string + st->ss.length + n, + st->ss.length - start - n); + st->ss.length -= n; + } + } + else + { + /* There are no characters to delete or no characters at or + beyond START, hence deletion is a no-op. */ + } +} + /* Returns true if ST is empty, false otherwise. */ bool ds_is_empty (const struct string *st) @@ -1413,3 +1441,25 @@ ds_relocate (struct string *st) free ((char *) rel); } } + + + + +/* Operations on uint8_t "strings" */ + +/* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes. + DST is truncated to DST_SIZE bytes or padded on the right with + copies of PAD as needed. */ +void +u8_buf_copy_rpad (uint8_t *dst, size_t dst_size, + const uint8_t *src, size_t src_size, + char pad) +{ + if (src_size >= dst_size) + memmove (dst, src, dst_size); + else + { + memmove (dst, src, src_size); + memset (&dst[src_size], pad, dst_size - src_size); + } +}