# HG changeset patch # User Jaroslav Hajek # Date 1263903894 -3600 # Node ID 2e4fc7fdba15c0492e740e1002bc480633c15cc1 # Parent aa0f575cf39b828131be7f9564608399f1507926 optimize strfind with 1 or 2 characters diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2010-01-19 Jaroslav Hajek + + * DLD-FUNCTIONS/strfind.cc: Optimize searching for 1 or 2 characters. + 2010-01-18 John W. Eaton * oct-parse.yy (fcn_end): Allow EOF as end of function if diff --git a/src/DLD-FUNCTIONS/strfind.cc b/src/DLD-FUNCTIONS/strfind.cc --- a/src/DLD-FUNCTIONS/strfind.cc +++ b/src/DLD-FUNCTIONS/strfind.cc @@ -70,8 +70,38 @@ // We'll use deque because it typically has the most favorable properties for // the operation we need. std::deque accum; - if (n >= m) + if (m == 1) + { + // Looking for a single character. + for (octave_idx_type i = 0; i < n; i++) + { + if (y[i] == x[0]) + accum.push_back (i); + } + } + else if (m == 2) { + // Two characters. + if (overlaps) + { + for (octave_idx_type i = 0; i < n-1; i++) + { + if (y[i] == x[0] && y[i+1] == x[1]) + accum.push_back (i); + } + } + else + { + for (octave_idx_type i = 0; i < n-1; i++) + { + if (y[i] == x[0] && y[i+1] == x[1]) + accum.push_back (i++); + } + } + } + else if (n >= m) + { + // General case. octave_idx_type j = 0; if (overlaps)