Răsfoiți Sursa

misc/cstring.h: add b_cstring_at()

ambrop7 13 ani în urmă
părinte
comite
d04ebbe211
1 a modificat fișierele cu 18 adăugiri și 0 ștergeri
  1. 18 0
      misc/cstring.h

+ 18 - 0
misc/cstring.h

@@ -97,6 +97,11 @@ static b_cstring b_cstring_make_empty (void);
  */
 static const char * b_cstring_get (b_cstring cstr, size_t offset, size_t maxlen, size_t *out_chunk_len);
 
+/**
+ * Retrieves the byte in the string at position \a pos.
+ */
+static char b_cstring_at (b_cstring cstr, size_t pos);
+
 /**
  * Asserts that the range given by \a offset and \a length is valid for the string.
  */
@@ -209,6 +214,19 @@ static const char * b_cstring_get (b_cstring cstr, size_t offset, size_t maxlen,
     return data;
 }
 
+static char b_cstring_at (b_cstring cstr, size_t pos)
+{
+    ASSERT(pos < cstr.length)
+    ASSERT(cstr.func)
+    
+    size_t chunk_len;
+    const char *data = cstr.func(&cstr, pos, &chunk_len);
+    ASSERT(data)
+    ASSERT(chunk_len > 0)
+    
+    return *data;
+}
+
 static void b_cstring_assert_range (b_cstring cstr, size_t offset, size_t length)
 {
     ASSERT(offset <= cstr.length)