commit 8951beee49a67cd6200814f698825a6cb3f8d5bf
parent f8537d27d94c3324c4025ed10e1a98ae09d6b856
Author: William Casarin <jb55@jb55.com>
Date: Sat, 22 Jul 2023 11:05:08 -0700
extract ndb_builder_try_compact_string
We're going to use this in our json string parsing as well
Diffstat:
M | nostrdb.c | | | 30 | +++++++++++++++++++++--------- |
1 file changed, 21 insertions(+), 9 deletions(-)
diff --git a/nostrdb.c b/nostrdb.c
@@ -56,6 +56,26 @@ int ndb_builder_new(struct ndb_builder *builder, unsigned char *buf,
return 1;
}
+/// Check for small strings to pack
+static inline int ndb_builder_try_compact_str(struct ndb_builder *builder,
+ const char *str, int len,
+ union packed_str *pstr)
+{
+ if (len == 0) {
+ *pstr = ndb_char_to_packed_str(0);
+ return 1;
+ } else if (len == 1) {
+ *pstr = ndb_char_to_packed_str(str[0]);
+ return 1;
+ } else if (len == 2) {
+ *pstr = ndb_chars_to_packed_str(str[0], str[1]);
+ return 1;
+ }
+
+ return 0;
+}
+
+
static inline int ndb_json_parser_init(struct ndb_json_parser *p,
const char *json, int json_len,
unsigned char *buf, int bufsize)
@@ -177,16 +197,8 @@ static int ndb_builder_push_unpacked_str(struct ndb_builder *builder,
int ndb_builder_make_str(struct ndb_builder *builder, const char *str, int len,
union packed_str *pstr)
{
- if (len == 0) {
- *pstr = ndb_char_to_packed_str(0);
- return 1;
- } else if (len == 1) {
- *pstr = ndb_char_to_packed_str(str[0]);
- return 1;
- } else if (len == 2) {
- *pstr = ndb_chars_to_packed_str(str[0], str[1]);
+ if (ndb_builder_try_compact_str(builder, str, len, pstr))
return 1;
- }
return ndb_builder_push_unpacked_str(builder, str, len, pstr);
}