Created
May 14, 2012 17:28
-
-
Save jaspervdj/2695201 to your computer and use it in GitHub Desktop.
Draft of the fast-aleck API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
/******************************************************************************* | |
* Builder * | |
*******************************************************************************/ | |
/* Smart and fast string concatenation which allows streaming */ | |
typedef struct { | |
/* ??? */ | |
} builder; | |
/* The obvious */ | |
inline void builder_write_char(builder *builder, char c); | |
inline void builder_write_string(builder *builder, char *str, size_t str_size); | |
/* Internall, the builder is structured as a queue of e.g., 32kb buffers. We | |
* always append to the last one, new buffers are created automatically when | |
* needed. Because it might be to expensive to check this each time you want to | |
* write a character, there is: | |
*/ | |
inline int builder_assert_size(builder *builder, size_t size); | |
/* which checks if the last builder has at least `size` bytes available, if not, | |
* create a new buffer so this is the case. | |
*/ | |
/* Whenever a new buffer is created, the previous one cannot be modified | |
* anymore. This means we can flush and free it if we want. | |
*/ | |
inline int builder_has_frozen_buffer(builder *builder); | |
char *builder_pop_frozen_buffer(builder *builder, size_t *buffer_size); | |
/* Get all the buffers! | |
*/ | |
char *builder_to_string(builder *builder, size_t *string_size); | |
/******************************************************************************* | |
* Fast aleck * | |
*******************************************************************************/ | |
typedef struct { | |
/* ??? */ | |
builder *builder; | |
} fast_aleck_state; | |
fast_aleck_feed(fast_aleck_state *state, char *input, size_t input_size); | |
/* The streaming approach will check `builder_has_frozen_buffer` once in a | |
* while. The in-memory approach will just call `builder_to_string` when it's | |
* done. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment