[!T] Added reduce
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#include "key_value_pair.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -130,3 +132,39 @@ void remove_key_value_pair(key_value_pair *kv, key_value_pair **head, key_value_
|
||||
iterator = iterator->next;
|
||||
}
|
||||
}
|
||||
|
||||
void extract_key_value_pairs(char *red_str, key_value_pair **head, key_value_pair **tail){
|
||||
char *it = red_str;
|
||||
bool reading_value = false;
|
||||
char *word_start = it;
|
||||
size_t word_length = 0;
|
||||
size_t value_length = 0;
|
||||
|
||||
while(*it){
|
||||
if(isalpha(*it) && reading_value){
|
||||
char key[word_length + 1];
|
||||
strncpy(key, word_start, word_length);
|
||||
char value[16];
|
||||
snprintf(value, 16, "%d", (int)value_length);
|
||||
|
||||
key_value_pair *kv = init_key_value_pair(key, value);
|
||||
add_key_value_pair(kv, head, tail);
|
||||
|
||||
// reset
|
||||
word_length = 0;
|
||||
value_length = 0;
|
||||
word_start = it;
|
||||
reading_value = false;
|
||||
continue;
|
||||
}
|
||||
if(isalpha(*it)){
|
||||
word_length++;
|
||||
it++;
|
||||
}
|
||||
else{
|
||||
reading_value = true; // reassigning true is faster than if checks every iteration
|
||||
word_length++;
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user