synonym.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*====================================================================*
  2. *
  3. * char const * synonym (char const * term, const struct _term_ list [], size_t size);
  4. *
  5. * symbol.h
  6. *
  7. * lookup term and return corresponding text; return the original
  8. * term if lookup fails; the list must be in lexographic order by
  9. * term or lookups may fail; struct _term_ is defined in types.h;
  10. *
  11. * Motley Tools by Charles Maier;
  12. * Copyright (c) 2001-2006 by Charles Maier Associates;
  13. * Licensed under the Internet Software Consortium License;
  14. *
  15. *--------------------------------------------------------------------*/
  16. #ifndef SYNONYM_SOURCE
  17. #define SYNONYM_SOURCE
  18. #include <string.h>
  19. #include "../tools/types.h"
  20. char const * synonym (char const * term, const struct _term_ list [], size_t size)
  21. {
  22. size_t lower = 0;
  23. size_t upper = size;
  24. while (lower < upper)
  25. {
  26. size_t index = (lower + upper) >> 1;
  27. signed order = strcmp (term, list [index].term);
  28. if (order < 0)
  29. {
  30. upper = index - 0;
  31. continue;
  32. }
  33. if (order > 0)
  34. {
  35. lower = index + 1;
  36. continue;
  37. }
  38. return (list [index].text);
  39. }
  40. return (term);
  41. }
  42. #endif