From 9fe35b1fc00d8a2ef3ec5e8b5adce0737fd82423 Mon Sep 17 00:00:00 2001 From: Hugues Delorme Date: Fri, 10 Jul 2015 09:52:20 +0200 Subject: [PATCH] gmio_core/internal: add gmio_string_buffer_clear() and gmio_copy_spaces() --- src/gmio_core/internal/string_parse.c | 5 ++-- src/gmio_core/internal/string_parse.h | 33 +++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/gmio_core/internal/string_parse.c b/src/gmio_core/internal/string_parse.c index fd45fbc..5e42604 100644 --- a/src/gmio_core/internal/string_parse.c +++ b/src/gmio_core/internal/string_parse.c @@ -32,14 +32,13 @@ int gmio_eat_word( { const size_t buffer_capacity = buffer->max_len; const char* stream_curr_char = NULL; - size_t i = 0; + size_t i = buffer->len; /* assert(buffer != NULL && buffer->ptr != NULL); */ - buffer->len = 0; stream_curr_char = gmio_skip_spaces(it); if (stream_curr_char == NULL) { /* Empty word */ - buffer->ptr[0] = 0; + buffer->ptr[i] = 0; return 0; } diff --git a/src/gmio_core/internal/string_parse.h b/src/gmio_core/internal/string_parse.h index 9124602..ec7e9ff 100644 --- a/src/gmio_core/internal/string_parse.h +++ b/src/gmio_core/internal/string_parse.h @@ -39,11 +39,13 @@ struct gmio_string_buffer { char* ptr; /*!< Buffer contents */ size_t len; /*!< Size(length) of current contents */ - size_t max_len; /*!< Maximum contents size(length) */ + size_t max_len; /*!< Maximum contents size(capacity) */ }; typedef struct gmio_string_buffer gmio_string_buffer_t; +GMIO_INLINE void gmio_string_buffer_clear(gmio_string_buffer_t* buffer); + /*! Forward iterator over a stream * * To be used with API below. @@ -76,10 +78,16 @@ GMIO_INLINE const char* gmio_current_char( GMIO_INLINE const char *gmio_next_char( gmio_string_stream_fwd_iterator_t *it); -/*! Advances iterator until the next non-space char */ +/*! Advances iterator until the first non-space char */ GMIO_INLINE const char* gmio_skip_spaces( gmio_string_stream_fwd_iterator_t* it); +/*! Advances iterator until the first non-space char and copies any space found + * in \p buffer */ +GMIO_INLINE void gmio_copy_spaces( + gmio_string_stream_fwd_iterator_t* it, + gmio_string_buffer_t* buffer); + /*! Advances iterator so that next word is extracted into \p buffer * * \retval 0 On success @@ -108,6 +116,12 @@ GMIO_INLINE int gmio_get_float32(const char* str, gmio_float32_t* value_ptr); * -- Implementation */ +void gmio_string_buffer_clear(gmio_string_buffer_t* buffer) +{ + buffer->ptr[0] = 0; + buffer->len = 0; +} + const char* gmio_current_char( const gmio_string_stream_fwd_iterator_t* it) { @@ -143,6 +157,21 @@ const char* gmio_skip_spaces( return curr_char; } +void gmio_copy_spaces( + gmio_string_stream_fwd_iterator_t* it, + gmio_string_buffer_t* buffer) +{ + const char* curr_char = gmio_current_char(it); + while (curr_char != NULL + && gmio_clocale_isspace(*curr_char) + && buffer->len < buffer->max_len) + { + buffer->ptr[buffer->len] = *curr_char; + curr_char = gmio_next_char(it); + ++buffer->len; + } +} + int gmio_get_float32(const char* str, gmio_float32_t* value_ptr) { #if defined(GMIO_STRINGPARSE_USE_FAST_ATOF)