1
////////////////////////////////////////////////////////////////////////////
2
// NoteCase notes manager project <http://notecase.sf.net>
4
// This code is licensed under BSD license.See "license.txt" for more details.
6
// File: Class implements fast text buffer search method
7
////////////////////////////////////////////////////////////////////////////
9
#include "TextSearch.h"
15
static gchar *utf8_strcasestr(const gchar *haystack, const gchar *needle);
17
TextSearch::TextSearch()
24
TextSearch::~TextSearch()
28
void TextSearch::SetScanStyle(unsigned long dwStyle)
30
//NOTE: you should NOT change style once the pattern is set
31
// (since that functions used current style setting)
32
//ASSERT(NULL == m_pszPattern && 0 == m_nPtrnSize);
37
void TextSearch::SetSearchPattern(const char *szText)
39
m_pszPattern = (const gchar *)szText;
40
m_nPtrnSize = g_utf8_strlen(szText, -1); //TOFIX if NULL
43
void TextSearch::SetSearchPattern(const char *szBinary, unsigned int nSize)
45
m_pszPattern = szBinary;
49
bool TextSearch::SetScanBuffer(const char *szBuffer, unsigned int nSize)
51
m_pszBlock = szBuffer;
53
return (m_pszBlock != NULL && m_nBlkSize > 0);
56
//TOFIX use int64 for large memory
57
long TextSearch::Search(unsigned long nStartPos)
60
*block = m_pszBlock, // Concrete pointer to block data
61
*pattern = m_pszPattern; // Concrete pointer to search value
63
ASSERT (NULL != block); // Expect non-NULL pointers, but
64
ASSERT (NULL != pattern); // fail gracefully if not debugging
66
if(nStartPos < 0 || nStartPos >= m_nBlkSize)
67
return -1; //invalid position
68
if (block == NULL || pattern == NULL)
71
//move position by the required amount of characters
72
block = g_utf8_offset_to_pointer(block, nStartPos);
74
// Pattern must be smaller or equal in size to string
75
if (m_nBlkSize-nStartPos < m_nPtrnSize)
76
return -1; // Otherwise it's not found
77
if (m_nPtrnSize == 0) // Empty patterns match at start
80
//NOTE: case sensitive and case insensitive version
82
if(m_dwStyle & FS_CASE_INSENSITIVE)
83
szPos = utf8_strcasestr(block, pattern);
85
szPos = g_strstr_len(block, m_nBlkSize - nStartPos, pattern);
88
long nPos = g_utf8_pointer_to_offset(block, szPos);
89
return nPos + nStartPos;
91
return -1; // Found nothing
94
void TextSearch::Clear()
103
int utf8_strnicmp(const gchar *szStr1, const gchar *szStr2, int nLen)
112
gunichar nC1 = g_unichar_toupper(g_utf8_get_char(szStr1));
113
gunichar nC2 = g_unichar_toupper(g_utf8_get_char(szStr2));
117
szStr1 = g_utf8_next_char(szStr1);
118
szStr2 = g_utf8_next_char(szStr2);
124
gchar *utf8_strcasestr(const gchar *haystack, const gchar *needle)
126
size_t haystack_len = g_utf8_strlen(haystack, -1);
127
size_t needle_len = g_utf8_strlen(needle, -1);
129
if (haystack_len < needle_len || needle_len == 0)
132
while (haystack_len >= needle_len) {
133
if (0 == utf8_strnicmp(haystack, needle, needle_len))
134
return (gchar *)haystack;
136
haystack = g_utf8_next_char(haystack);
1
////////////////////////////////////////////////////////////////////////////
2
// NoteCase notes manager project <http://notecase.sf.net>
4
// This code is licensed under BSD license.See "license.txt" for more details.
6
// File: Class implements fast text buffer search method
7
////////////////////////////////////////////////////////////////////////////
9
#include "TextSearch.h"
15
static gchar *utf8_strcasestr(const gchar *haystack, const gchar *needle);
17
TextSearch::TextSearch()
24
TextSearch::~TextSearch()
28
void TextSearch::SetScanStyle(unsigned long dwStyle)
30
//NOTE: you should NOT change style once the pattern is set
31
// (since that functions used current style setting)
32
//ASSERT(NULL == m_pszPattern && 0 == m_nPtrnSize);
37
void TextSearch::SetSearchPattern(const char *szText)
39
m_pszPattern = (const gchar *)szText;
40
m_nPtrnSize = g_utf8_strlen(szText, -1); //TOFIX if NULL
43
void TextSearch::SetSearchPattern(const char *szBinary, unsigned int nSize)
45
m_pszPattern = szBinary;
49
bool TextSearch::SetScanBuffer(const char *szBuffer, unsigned int nSize)
51
m_pszBlock = szBuffer;
53
return (m_pszBlock != NULL && m_nBlkSize > 0);
56
//TOFIX use int64 for large memory
57
long TextSearch::Search(unsigned long nStartPos)
60
*block = m_pszBlock, // Concrete pointer to block data
61
*pattern = m_pszPattern; // Concrete pointer to search value
63
ASSERT (NULL != block); // Expect non-NULL pointers, but
64
ASSERT (NULL != pattern); // fail gracefully if not debugging
66
if(nStartPos < 0 || nStartPos >= m_nBlkSize)
67
return -1; //invalid position
68
if (block == NULL || pattern == NULL)
71
//move position by the required amount of characters
72
block = g_utf8_offset_to_pointer(block, nStartPos);
74
// Pattern must be smaller or equal in size to string
75
if (m_nBlkSize-nStartPos < m_nPtrnSize)
76
return -1; // Otherwise it's not found
77
if (m_nPtrnSize == 0) // Empty patterns match at start
80
//NOTE: case sensitive and case insensitive version
82
if(m_dwStyle & FS_CASE_INSENSITIVE)
83
szPos = utf8_strcasestr(block, pattern);
85
szPos = g_strstr_len(block, m_nBlkSize - nStartPos, pattern);
88
long nPos = g_utf8_pointer_to_offset(block, szPos);
89
return nPos + nStartPos;
91
return -1; // Found nothing
94
void TextSearch::Clear()
103
int utf8_strnicmp(const gchar *szStr1, const gchar *szStr2, int nLen)
112
gunichar nC1 = g_unichar_toupper(g_utf8_get_char(szStr1));
113
gunichar nC2 = g_unichar_toupper(g_utf8_get_char(szStr2));
117
szStr1 = g_utf8_next_char(szStr1);
118
szStr2 = g_utf8_next_char(szStr2);
124
gchar *utf8_strcasestr(const gchar *haystack, const gchar *needle)
126
size_t haystack_len = g_utf8_strlen(haystack, -1);
127
size_t needle_len = g_utf8_strlen(needle, -1);
129
if (haystack_len < needle_len || needle_len == 0)
132
while (haystack_len >= needle_len) {
133
if (0 == utf8_strnicmp(haystack, needle, needle_len))
134
return (gchar *)haystack;
136
haystack = g_utf8_next_char(haystack);