From b7f56459d566e232a554ab29bcf1015b303d59b1 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 24 Nov 2009 01:18:11 -0500 Subject: Make string lexing O(n). --- dimension/flex.l | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'dimension') diff --git a/dimension/flex.l b/dimension/flex.l index 8c3008d..2e2467c 100644 --- a/dimension/flex.l +++ b/dimension/flex.l @@ -71,22 +71,27 @@ #define STRING_TOKEN() \ do { \ NEW_TOKEN(DMNSN_T_STRING); \ - token.value = malloc(1); \ - token.value[0] = '\0'; \ string_length = 0; \ + string_extent = 8; \ + token.value = malloc(string_extent); \ + token.value[0] = '\0'; \ CALCULATE_COLUMN(); \ } while (0) #define STRCAT(str, len) \ do { \ - token.value = realloc(token.value, string_length + len + 1); \ + if (string_length + len + 1 >= string_length) { \ + string_extent = 2*(string_length + len + 1); \ + token.value = realloc(token.value, string_extent); \ + } \ + \ strncpy(token.value + string_length, str, len + 1); \ string_length += len; \ CALCULATE_COLUMN(); \ } while(0) dmnsn_token token; -size_t string_length; +size_t string_length, string_extent; unsigned long wchar; %} -- cgit v1.2.3