]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/keyword.cc
lilypond-manuals.css: edit color scheme and some spacing
[lilypond.git] / lily / keyword.cc
index 4eec8b4f72fe843d5002f645ac4de10f4d4ee944..b5f7947ada203e31550ee041a557f37f439b115c 100644 (file)
@@ -1,76 +1,34 @@
 /*
   keyword.cc -- keywords and identifiers
- */
-
-#include <stdlib.h>
-
-#include "my-lily-lexer.hh"
-
-/* for the keyword table */
-struct Keyword_ent
-{
-    char const *name;
-    int     tokcode;
-};
+*/
 
-struct Keyword_table
-{
-    Keyword_ent *table;
-    int     maxkey;
-    Keyword_table (Keyword_ent *);
-    int     lookup (char const *s) const;
-};
+#include "keyword.hh"
 
+#include <cstring>
+#include <cstdlib>
+using namespace std;
 
 /* for qsort */
-int
-        tabcmp (void const * p1, void const * p2)
+bool tab_less (Keyword_ent const &p1, Keyword_ent const &p2)
 {
-    return strcmp (((Keyword_ent const *) p1)->name,
-                  ((Keyword_ent const *) p2)->name);
+  return strcmp (p1.name_, p2.name_) < 0;
 }
 
 Keyword_table::Keyword_table (Keyword_ent *tab)
 {
-    table = tab;
-
-    /* count keywords */
-    for (maxkey = 0; table[maxkey].name; maxkey++);
+  while (tab->name_)
+    table_.push_back (*tab++);
 
-    /* sort them */
-    qsort (table, maxkey, sizeof (Keyword_ent), tabcmp);
+  vector_sort (table_, tab_less);
 }
 
-/*
-  lookup with binsearch, return tokencode.
-*/
-int
-Keyword_table::lookup (char const *s)const
+vsize
+Keyword_table::lookup (char const *s) const
 {
-    int     lo,
-            hi,
-            cmp,
-            result;
-    lo = 0;
-    hi = maxkey;
-
-    /* binary search */
-    do
-    {
-        cmp = (lo + hi) / 2;
-
-        result = strcmp (s, table[cmp].name);
-
-        if (result < 0)
-            hi = cmp;
-        else
-            lo = cmp;
-    }
-    while (hi - lo > 1);
-    if (!strcmp (s, table[lo].name))
-    {
-        return table[lo].tokcode;
-    } else
-        return -1;              /* not found */
+  Keyword_ent e;
+  e.name_ = s;
+  vsize idx = binary_search (table_, e, tab_less);
+  if (idx != VPOS)
+    return table_[idx].tokcode_;
+  return VPOS;
 }
-