19 #include <sys/types.h>
29 KRegExpPrivate::KRegExpPrivate()
33 for (
int i = 0; i < 10; i++ )
37 KRegExpPrivate::KRegExpPrivate(
const char *_pattern,
const char *_mode )
41 for (
int i = 0; i < 10; i++ )
44 compile( _pattern, _mode );
47 KRegExpPrivate::~KRegExpPrivate()
49 for (
int i = 0; i < 10; i++ )
50 if ( m_strMatches[i] )
51 free( m_strMatches[i] );
54 regfree( &m_pattern );
57 bool KRegExpPrivate::compile(
const char *_pattern,
const char *_mode )
60 regfree( &m_pattern );
62 int res = regcomp( &m_pattern, _pattern, ( strchr( _mode,
'i' ) != 0L ? REG_ICASE : 0 ) | REG_EXTENDED );
69 bool KRegExpPrivate::match(
const char *_string )
73 kdDebug(128) <<
"You must compile a pattern before you can try to match it" <<
endl;
77 for (
int i = 0; i < 10; i++ )
79 m_matches[i].rm_so = -1;
80 m_matches[i].rm_eo = -1;
81 if ( m_strMatches[i] )
83 free( m_strMatches[i] );
88 int res = regexec( &m_pattern, _string, 10, m_matches, 0 );
92 int slen = strlen( _string );
94 for (
int j = 0; j < 10; j++ )
96 if ( m_matches[j].rm_so >= 0 && m_matches[j].rm_eo >= 0 &&
97 m_matches[j].rm_so <= slen && m_matches[j].rm_eo <= slen &&
98 m_matches[j].rm_so <= m_matches[j].rm_eo )
100 int len = m_matches[j].rm_eo - m_matches[j].rm_so;
101 m_strMatches[j] = (
char* )malloc( len + 1 );
102 memcpy( m_strMatches[j], _string + m_matches[j].rm_so, len );
103 m_strMatches[j][ len ] = 0;
110 const char* KRegExpPrivate::group(
int _grp )
112 if ( _grp < 0 || _grp >= 10 )
114 kdDebug(128) <<
"You may only use a group in the range of 0..9" <<
endl;
118 return m_strMatches[ _grp ];
121 int KRegExpPrivate::groupStart(
int _grp )
123 if ( _grp < 0 || _grp >= 10 )
125 kdDebug(128) <<
"You may only use a group in the range of 0..9" <<
endl;
129 return m_matches[ _grp ].rm_so;
132 int KRegExpPrivate::groupEnd(
int _grp )
134 if ( _grp < 0 || _grp >= 10 )
136 kdDebug(128) <<
"You may only use a group in the range of 0..9" <<
endl;
140 return m_matches[ _grp ].rm_eo;
160 return m_pPrivate->compile( _pattern, _mode );
165 return m_pPrivate->match( _string );
170 return m_pPrivate->group( _grp );
175 return m_pPrivate->groupStart( _grp );
180 return m_pPrivate->groupEnd( _grp );
Used internally by KRegExp.
KRegExp()
Creates a KRegExp object without a default pattern.
int groupStart(int _grp)
The offset of the given group in the string.
bool match(const char *_string)
Match a string to the last supplied regexp.
int groupEnd(int _grp)
The offset of the given group's end in the string.
const char * group(int _grp)
Returns a group from the match.
bool compile(const char *_pattern, const char *_mode="")
Prepare a regular expression for subsequent matches.
kndbgstream & endl(kndbgstream &s)
Does nothing.