Qt 4.8
Functions
qstringmatcher.cpp File Reference
#include "qstringmatcher.h"

Go to the source code of this file.

Functions

static int bm_find (const ushort *uc, uint l, int index, const ushort *puc, uint pl, const uchar *skiptable, Qt::CaseSensitivity cs)
 
static void bm_init_skiptable (const ushort *uc, int len, uchar *skiptable, Qt::CaseSensitivity cs)
 
int qFindStringBoyerMoore (const QChar *haystack, int haystackLen, int haystackOffset, const QChar *needle, int needleLen, Qt::CaseSensitivity cs)
 

Function Documentation

◆ bm_find()

static int bm_find ( const ushort uc,
uint  l,
int  index,
const ushort puc,
uint  pl,
const uchar skiptable,
Qt::CaseSensitivity  cs 
)
inlinestatic

Definition at line 65 of file qstringmatcher.cpp.

Referenced by QStringMatcher::indexIn(), and qFindStringBoyerMoore().

67 {
68  if (pl == 0)
69  return index > (int)l ? -1 : index;
70  const uint pl_minus_one = pl - 1;
71 
72  register const ushort *current = uc + index + pl_minus_one;
73  const ushort *end = uc + l;
74  if (cs == Qt::CaseSensitive) {
75  while (current < end) {
76  uint skip = skiptable[*current & 0xff];
77  if (!skip) {
78  // possible match
79  while (skip < pl) {
80  if (*(current - skip) != puc[pl_minus_one-skip])
81  break;
82  skip++;
83  }
84  if (skip > pl_minus_one) // we have a match
85  return (current - uc) - pl_minus_one;
86 
87  // in case we don't have a match we are a bit inefficient as we only skip by one
88  // when we have the non matching char in the string.
89  if (skiptable[*(current - skip) & 0xff] == pl)
90  skip = pl - skip;
91  else
92  skip = 1;
93  }
94  if (current > end - skip)
95  break;
96  current += skip;
97  }
98  } else {
99  while (current < end) {
100  uint skip = skiptable[foldCase(current, uc) & 0xff];
101  if (!skip) {
102  // possible match
103  while (skip < pl) {
104  if (foldCase(current - skip, uc) != foldCase(puc + pl_minus_one - skip, puc))
105  break;
106  skip++;
107  }
108  if (skip > pl_minus_one) // we have a match
109  return (current - uc) - pl_minus_one;
110  // in case we don't have a match we are a bit inefficient as we only skip by one
111  // when we have the non matching char in the string.
112  if (skiptable[foldCase(current - skip, uc) & 0xff] == pl)
113  skip = pl - skip;
114  else
115  skip = 1;
116  }
117  if (current > end - skip)
118  break;
119  current += skip;
120  }
121  }
122  return -1; // not found
123 }
unsigned int uint
Definition: qglobal.h:996
static uint foldCase(const ushort *ch, const ushort *start)
Definition: qchar.cpp:1380
unsigned short ushort
Definition: qglobal.h:995
QFactoryLoader * l
quint16 index
static const KeyPair *const end

◆ bm_init_skiptable()

static void bm_init_skiptable ( const ushort uc,
int  len,
uchar skiptable,
Qt::CaseSensitivity  cs 
)
static

Definition at line 46 of file qstringmatcher.cpp.

Referenced by qFindStringBoyerMoore(), QStringMatcher::QStringMatcher(), QStringMatcher::setCaseSensitivity(), and QStringMatcher::setPattern().

47 {
48  int l = qMin(len, 255);
49  memset(skiptable, l, 256*sizeof(uchar));
50  uc += len - l;
51  if (cs == Qt::CaseSensitive) {
52  while (l--) {
53  skiptable[*uc & 0xff] = l;
54  uc++;
55  }
56  } else {
57  const ushort *start = uc;
58  while (l--) {
59  skiptable[foldCase(uc, start) & 0xff] = l;
60  uc++;
61  }
62  }
63 }
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
unsigned char uchar
Definition: qglobal.h:994
static uint foldCase(const ushort *ch, const ushort *start)
Definition: qchar.cpp:1380
unsigned short ushort
Definition: qglobal.h:995
QFactoryLoader * l

◆ qFindStringBoyerMoore()

int qFindStringBoyerMoore ( const QChar haystack,
int  haystackLen,
int  haystackOffset,
const QChar needle,
int  needleLen,
Qt::CaseSensitivity  cs 
)
Warning
This function is not part of the public interface.

Definition at line 322 of file qstringmatcher.cpp.

Referenced by qFindString().

325 {
326  uchar skiptable[256];
327  bm_init_skiptable((const ushort *)needle, needleLen, skiptable, cs);
328  if (haystackOffset < 0)
329  haystackOffset = 0;
330  return bm_find((const ushort *)haystack, haystackLen, haystackOffset,
331  (const ushort *)needle, needleLen, skiptable, cs);
332 }
static void bm_init_skiptable(const ushort *uc, int len, uchar *skiptable, Qt::CaseSensitivity cs)
unsigned char uchar
Definition: qglobal.h:994
static int bm_find(const ushort *uc, uint l, int index, const ushort *puc, uint pl, const uchar *skiptable, Qt::CaseSensitivity cs)
unsigned short ushort
Definition: qglobal.h:995