r80465 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80464‎ | r80465 | r80466 >
Date:21:14, 17 January 2011
Author:platonides
Status:deferred
Tags:
Comment:
Make a bunch of functions static.
Added a binary-safe strpos()
Fixed an interface bug with STRSTR and strpos().
Remove findSpaceOrAngle(), unused since r80461.
Modified paths:
  • /trunk/extensions/NativePreprocessor/preprocesstoobj.c (modified) (history)

Diff [purge]

Index: trunk/extensions/NativePreprocessor/preprocesstoobj.c
@@ -12,18 +12,22 @@
1313
1414 #define PTD_FOR_INCLUSION 1 /* Matches Parser::PTD_FOR_INCLUSION */
1515
16 -// FIXME: Do not rely on the terminating \0
17 -#define STRSTR(haystack, needle) strpos(haystack, needle, 0)
18 -int strpos(const char* haystack, const char* needle, int offset) {
19 - char* s = strstr(haystack+offset, needle);
20 - if (!s) return -1;
21 - return s - haystack;
 16+static int strpos(const char* haystack, int haystack_len, const char* needle, int needle_len, int offset) {
 17+ int i;
 18+
 19+ for ( i = offset; i < haystack_len - needle_len; i++ ) {
 20+ if ( !memcmp( haystack + i, needle, needle_len ) ) {
 21+ return i;
 22+ }
 23+ }
 24+
 25+ return -1;
2226 }
2327
2428 #define strsize(x) (sizeof(x)-1)
2529 #define min(x,y) (((x) < (y)) ? (x) : (y))
2630
27 -enum internalTags getInternalTag(const char* name, int name_len) {
 31+static enum internalTags getInternalTag(const char* name, int name_len) {
2832 #define CHECK_INTERNAL_TAG(x) if ((sizeof(#x)-1 == name_len) && !strncasecmp(name, #x, sizeof(#x)-1)) return x;
2933 if (name[0] == '/') {
3034 name++;
@@ -52,7 +56,7 @@
5357 #define searchReset() strcpy(search, "[{<\n") // $search = $searchBase;
5458 #define addSearch(x) addToSearch(search, sizeof(search), x) // $search .= 'x';
5559 #define MAX_SEARCH_CHARS "[{<\n|=}]"
56 -void addToSearch(char* search, int search_len, char x) {
 60+static void addToSearch(char* search, int search_len, char x) {
5761 int e;
5862 assert(strchr(MAX_SEARCH_CHARS, x));
5963 e = strlen(search);
@@ -70,7 +74,7 @@
7175 /**
7276 * Counts the number of times the character c appears since start, up to length.
7377 */
74 -int chrspn( const char* text, int c, int start, int length ) {
 78+static int chrspn( const char* text, int c, int start, int length ) {
7579 int i;
7680 for (i=0; i < length; i++) {
7781 if ( text[start+i] != c ) {
@@ -81,27 +85,6 @@
8286 }
8387
8488 /**
85 - * Return the first index in text that either matches a PCRE \s or a '<'
86 - * Returns -1 if not found. Remember that for PERL compatibility, \s doesn't
87 - * include the Vertical Tab (0x11)
88 - */
89 -int findSpaceOrAngle(const char* text, int text_len) {
90 - int i;
91 - for (i = 0; i < text_len; i++) {
92 - switch ( text[i] ) {
93 - case '\t':
94 - case '\n':
95 - case '\f':
96 - case '\r':
97 - case ' ':
98 - case '>':
99 - return i;
100 - }
101 - }
102 - return -1;
103 -}
104 -
105 -/**
10689 * Locates an end tag for the given tag name.
10790 * Matches the regex "/<\/$name\s*>/i"
10891 * Doesn't (completely) support tag names which contain '<'
@@ -140,7 +123,7 @@
141124 /**
142125 * Returns the number of times the character c appears in text, searching backwards from position start
143126 */
144 -int chrrspn( const char* text, int c, int start ) {
 127+static int chrrspn( const char* text, int c, int start ) {
145128 int i = 0;
146129 while ( ( start-i >= 0 ) && text[start-i] == c ) {
147130 i++;
@@ -181,7 +164,7 @@
182165 if ( forInclusion ) {
183166 /* $ignoredTags = array( 'includeonly', '/includeonly' ); */
184167 ignoredElement = noinclude;
185 - if ( STRSTR( text, "<onlyinclude>" ) && STRSTR( text, "</onlyinclude>" ) ) {
 168+ if ( strpos( text, text_len, "<onlyinclude>", 13, 0 ) != -1 && strpos( text, text_len, "</onlyinclude>", 14, 0 ) != -1 ) {
186169 enableOnlyinclude = true;
187170 }
188171 } else {
@@ -213,7 +196,7 @@
214197
215198 if ( findOnlyinclude ) {
216199 // Ignore all input up to the next <onlyinclude>
217 - int startPos = strpos( text, "<onlyinclude>", i );
 200+ int startPos = strpos( text, text_len, "<onlyinclude>", 13, i );
218201 if ( startPos == -1 ) {
219202 // Ignored section runs to the end
220203 addNodeWithText(ignore_node, text, i, -1);
@@ -323,7 +306,7 @@
324307 // trailing spaces and one of the newlines.
325308
326309 // Find the end
327 - int endPos = strpos( text, "-->", i + 4 );
 310+ int endPos = strpos( text, text_len, "-->", 3, i + 4 );
328311 if ( endPos == -1 ) {
329312 // Unclosed comment in input, runs to end
330313 addNodeWithText(comment_node, text, i, -1);

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r80461Follow up r80376. Added missing file FORMAT....platonides19:54, 17 January 2011

Status & tagging log