diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2010-03-29 16:37:38 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2010-03-29 16:37:38 +0200 |
commit | 51913ccf136aff6b01b19fd13078b726cde15d6f (patch) | |
tree | 28eada9709b17d29bc03d54dcc44606b65b406e1 /libxsde/xsde/cxx/string-search.cxx | |
parent | 71e026399d644c3e46aff5cab5e92c8ac56f0c79 (diff) |
Use binary search to check for string enumerations
Diffstat (limited to 'libxsde/xsde/cxx/string-search.cxx')
-rw-r--r-- | libxsde/xsde/cxx/string-search.cxx | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/libxsde/xsde/cxx/string-search.cxx b/libxsde/xsde/cxx/string-search.cxx new file mode 100644 index 0000000..10da5f0 --- /dev/null +++ b/libxsde/xsde/cxx/string-search.cxx @@ -0,0 +1,41 @@ +// file : xsde/cxx/string-search.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include <string.h> // strcmp +#include <xsde/cxx/string-search.hxx> + +namespace xsde +{ + namespace cxx + { + size_t + search (const char* const* array, size_t size, const char* s) + { + if (size == 0) + return 0; + + int r (1); + size_t m; + size_t l = 0; + size_t h = size - 1; + + while (l <= h) + { + m = l + (h - l) / 2; + r = strcmp (array[m], s); + + if (r == 0) + break; + + if (r < 0) + l = m + 1; + else + h = m - 1; + } + + return r == 0 ? m : size; + } + } +} |