/* * Copyright (c) 1995, 1996 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "pg_config.h" IDENT("@(#) prefices.cc (Gunther Schadow) 12/19/96"); #include "Unit.h" #include const int prefices::not_found = 12345; const int prefices::best_fit = -1; const int maxpfx = 16; prefices::prefix_s prefices::tab[maxpfx] = { /* * THE FOLLOWING LINES MUST BE REVERSELY ALPHABETICALLY SORTED */ {"u", 1, -6}, {"t", 1, 12}, {"pe",2, 15}, {"p", 1,-12}, {"n", 1, -9}, {"ma",2, 6}, {"m", 1, -3}, {"k", 1, 3}, {"h", 1, 2}, {"g", 1, 9}, {"f", 1,-15}, {"ex",2, 18}, {"da",2, 1}, {"d", 1, -1}, {"c", 1, -2}, {"a", 1,-18} }; /* * Lookup a key of exact length len, at the beginning of string s. * If successful return the exponent, and set r to the rootstring * after the prefix. */ int prefices::lookup(const char *s, int l, const char **r) { if(l == 0) { *r = s; return 0; } for(int i = 0; i < maxpfx; i++) { struct prefix_s entry = tab[i]; if(s[0] == entry.name[0] && entry.len == l) { if(strncmp(s, entry.name, l) == 0) { *r = s + l; return entry.exponent; } } else if(s[0] > entry.name[0]) return not_found; // not found } return not_found; } /* * Lookup a key of maximum length len, at the beginning of string s. * If successful return the exponent, set l to the actual length of * the prefix and r to the rootstring after the prefix. If len is * best_fit at the beginning look for the best fit. */ int prefices::lookup(const char *s, int *l, const char **r) { if(*l == 0) { *r = s; return 0; } int maxlen = *l; for(int i = 0; i < maxpfx; i++) { struct prefix_s entry = tab[i]; if(s[0] == entry.name[0]) { if(maxlen == best_fit) maxlen = entry.len; if(entry.len <= maxlen) { if(strncmp(s, entry.name, entry.len) == 0) { *l = entry.len; *r = s + entry.len; return entry.exponent; } } } else if(s[0] > entry.name[0]) return not_found; // not found } return not_found; }