--- parser3/src/main/pa_dictionary.C 2003/03/20 11:11:55 1.17.2.6.2.3 +++ parser3/src/main/pa_dictionary.C 2023/09/26 20:49:10 1.30 @@ -1,56 +1,77 @@ /** @file Parser: dictionary class impl. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ -static const char* IDENT_DICTIONARY_C="$Date: 2003/03/20 11:11:55 $"; - #include "pa_dictionary.h" #include "pa_exception.h" -// could not make this static: gcc complains "prev decl was extern" -void pa_dictionary_add_first(Table::element_type row, Dictionary* self) { - // get a=>b values - const String* a=row->get(0); - // empty 'a' check - if(!*a) { - throw Exception("parser.runtime", - a, - "dictionary table 'from' column elements must not be empty"); +volatile const char * IDENT_PA_DICTIONARY_C="$Id: pa_dictionary.C,v 1.30 2023/09/26 20:49:10 moko Exp $" IDENT_PA_DICTIONARY_H; + +Dictionary::Dictionary(Table& atable): substs(atable.count()) { + // clear starting_lines + memset(starting_line_of, 0, sizeof(starting_line_of)); + // grab first letters of first column of a table + constructor_line=1; + + for(Array_iterator i(atable); i.has_next(); ) { + ArrayString* row=i.next(); + + append_subst( + row->get(0), + (row->count()>1) ? row->get(1) : 0, + "dictionary table 'from' column elements must not be empty" + ); } - unsigned char c=(unsigned char)a->first_char(); - if(!self->starting_line_of[c]) - self->starting_line_of[c]=self->constructor_line; +} - self->constructor_line++; +Dictionary::Dictionary(const String& from, const String& to): substs(1) { + // clear starting_lines + memset(starting_line_of, 0, sizeof(starting_line_of)); + constructor_line=1; + + append_subst(&from, &to); } -Dictionary::Dictionary(Table& atable): table(atable) { - // grab first letters of first column of a table - constructor_line=1; table.for_each(pa_dictionary_add_first, this); + + +void Dictionary::append_subst(const String* from, const String* to, const char* exception_cstr) { + if(from->is_empty()) + throw Exception(PARSER_RUNTIME, + 0, + exception_cstr ? exception_cstr : "'from' must not be empty"); + + // record simplier 'from' for quick comparisons in 'starts' extremely-tight-callback + substs+=Dictionary::Subst(from->cstr(), (to && !to->is_empty()) ? to : 0); + + unsigned char c=(unsigned char)from->first_char(); + if(!starting_line_of[c]) + starting_line_of[c]=constructor_line; + + constructor_line++; } #ifndef DOXYGEN -struct First_that_starts_info { +struct First_that_begins_info { int line; const char* str; }; #endif -static bool starts(Table::element_type row, First_that_starts_info* info) { +static bool starts(Dictionary::Subst subst, First_that_begins_info* info) { // skip irrelevant lines if(info->line>1) { --info->line; return 0; } - return row->get(0)->this_starts(info->str); + return strncmp(subst.from, info->str, subst.from_length)==0; } -Table::element_type Dictionary::first_that_starts(const char* str) const { - First_that_starts_info info; - if(info.line=starting_line_of[(unsigned char)*str]) { +Dictionary::Subst Dictionary::first_that_begins(const char* str) const { + First_that_begins_info info; + if((info.line=starting_line_of[(unsigned char)*str])) { info.str=str; - return table.first_that(starts, &info); + return substs.first_that(starts, &info); } else - return Table::element_type(0); + return 0; }