Fossil SCM
Minor code clean-up of src/markdown.c: add a few 'const' specifiers, reduce the scope of temporary variables and simplify their names.
Commit
b9393a4e640d3ab23dcf0ad94df5f54ac06b707b302ea15a471d764562b6d784
Parent
ebce0f357e0732c…
1 file changed
+16
-18
+16
-18
| --- src/markdown.c | ||
| +++ src/markdown.c | ||
| @@ -116,11 +116,11 @@ | ||
| 116 | 116 | **********************/ |
| 117 | 117 | |
| 118 | 118 | /* markdown -- parses the input buffer and renders it into the output buffer */ |
| 119 | 119 | void markdown( |
| 120 | 120 | struct Blob *ob, |
| 121 | - struct Blob *ib, | |
| 121 | + const struct Blob *ib, | |
| 122 | 122 | const struct mkd_renderer *rndr); |
| 123 | 123 | |
| 124 | 124 | |
| 125 | 125 | #endif /* INTERFACE */ |
| 126 | 126 | |
| @@ -2113,11 +2113,11 @@ | ||
| 2113 | 2113 | * REFERENCE PARSING * |
| 2114 | 2114 | *********************/ |
| 2115 | 2115 | |
| 2116 | 2116 | /* is_ref -- returns whether a line is a reference or not */ |
| 2117 | 2117 | static int is_ref( |
| 2118 | - char *data, /* input text */ | |
| 2118 | + const char *data, /* input text */ | |
| 2119 | 2119 | size_t beg, /* offset of the beginning of the line */ |
| 2120 | 2120 | size_t end, /* offset of the end of the text */ |
| 2121 | 2121 | size_t *last, /* last character of the link */ |
| 2122 | 2122 | struct Blob *refs /* array of link references */ |
| 2123 | 2123 | ){ |
| @@ -2240,15 +2240,15 @@ | ||
| 2240 | 2240 | * FOOTNOTE PARSING * |
| 2241 | 2241 | *********************/ |
| 2242 | 2242 | |
| 2243 | 2243 | /* is_footnote -- returns whether a line is a footnote or not */ |
| 2244 | 2244 | static int is_footnote( |
| 2245 | - char *data, /* input text */ | |
| 2245 | + const char *data, /* input text */ | |
| 2246 | 2246 | size_t beg, /* offset of the beginning of the line */ |
| 2247 | 2247 | size_t end, /* offset of the end of the text */ |
| 2248 | 2248 | size_t *last, /* last character of the link */ |
| 2249 | - struct Blob * footnotes /* FIXME: struct render *rndr */ | |
| 2249 | + struct Blob * footnotes | |
| 2250 | 2250 | ){ |
| 2251 | 2251 | size_t i = 0; |
| 2252 | 2252 | size_t id_offset, id_end; |
| 2253 | 2253 | size_t note_offset, note_end; |
| 2254 | 2254 | size_t line_end; |
| @@ -2304,19 +2304,18 @@ | ||
| 2304 | 2304 | **********************/ |
| 2305 | 2305 | |
| 2306 | 2306 | /* markdown -- parses the input buffer and renders it into the output buffer */ |
| 2307 | 2307 | void markdown( |
| 2308 | 2308 | struct Blob *ob, /* output blob for rendered text */ |
| 2309 | - struct Blob *ib, /* input blob in markdown */ | |
| 2309 | + const struct Blob *ib, /* input blob in markdown */ | |
| 2310 | 2310 | const struct mkd_renderer *rndrer /* renderer descriptor (callbacks) */ |
| 2311 | 2311 | ){ |
| 2312 | 2312 | struct link_ref *lr; |
| 2313 | 2313 | struct footnote *fn; |
| 2314 | 2314 | size_t i, beg, end = 0; |
| 2315 | 2315 | struct render rndr; |
| 2316 | - char *ib_data; | |
| 2317 | - Blob text = BLOB_INITIALIZER; | |
| 2316 | + Blob text = BLOB_INITIALIZER; /* input after the first pass */ | |
| 2318 | 2317 | |
| 2319 | 2318 | /* filling the render structure */ |
| 2320 | 2319 | if( !rndrer ) return; |
| 2321 | 2320 | rndr.make = *rndrer; |
| 2322 | 2321 | rndr.nBlobCache = 0; |
| @@ -2339,31 +2338,30 @@ | ||
| 2339 | 2338 | if( rndr.make.image || rndr.make.link ) rndr.active_char['['] = char_link; |
| 2340 | 2339 | rndr.active_char['<'] = char_langle_tag; |
| 2341 | 2340 | rndr.active_char['\\'] = char_escape; |
| 2342 | 2341 | rndr.active_char['&'] = char_entity; |
| 2343 | 2342 | |
| 2344 | - /* first pass: looking for references, copying everything else */ | |
| 2343 | + /* first pass: iterate over lines looking for references, | |
| 2344 | + * copying everything else into "text" */ | |
| 2345 | 2345 | beg = 0; |
| 2346 | - ib_data = blob_buffer(ib); | |
| 2347 | - while( beg<blob_size(ib) ){ /* iterating over lines */ | |
| 2348 | - if( is_ref(ib_data, beg, blob_size(ib), &end, &rndr.refs) ){ | |
| 2346 | + for(const size_t size = blob_size(ib); beg<size ;){ | |
| 2347 | + const char* const data = blob_buffer(ib); | |
| 2348 | + if( is_ref(data, beg, size, &end, &rndr.refs) ){ | |
| 2349 | 2349 | beg = end; |
| 2350 | - }else if( is_footnote(ib_data, beg, blob_size(ib), &end, &rndr.footnotes) ){ | |
| 2350 | + }else if( is_footnote(data, beg, size, &end, &rndr.footnotes) ){ | |
| 2351 | 2351 | /* FIXME: fossil_print("\nfootnote found at %i\n", beg); */ |
| 2352 | 2352 | beg = end; |
| 2353 | 2353 | }else{ /* skipping to the next line */ |
| 2354 | 2354 | end = beg; |
| 2355 | - while( end<blob_size(ib) && ib_data[end]!='\n' && ib_data[end]!='\r' ){ | |
| 2355 | + while( end<size && data[end]!='\n' && data[end]!='\r' ){ | |
| 2356 | 2356 | end += 1; |
| 2357 | 2357 | } |
| 2358 | 2358 | /* adding the line body if present */ |
| 2359 | - if( end>beg ) blob_append(&text, ib_data + beg, end - beg); | |
| 2360 | - while( end<blob_size(ib) && (ib_data[end]=='\n' || ib_data[end]=='\r') ){ | |
| 2359 | + if( end>beg ) blob_append(&text, data + beg, end - beg); | |
| 2360 | + while( end<size && (data[end]=='\n' || data[end]=='\r') ){ | |
| 2361 | 2361 | /* add one \n per newline */ |
| 2362 | - if( ib_data[end]=='\n' | |
| 2363 | - || (end+1<blob_size(ib) && ib_data[end+1]!='\n') | |
| 2364 | - ){ | |
| 2362 | + if( data[end]=='\n' || (end+1<size && data[end+1]!='\n') ){ | |
| 2365 | 2363 | blob_append_char(&text, '\n'); |
| 2366 | 2364 | } |
| 2367 | 2365 | end += 1; |
| 2368 | 2366 | } |
| 2369 | 2367 | beg = end; |
| 2370 | 2368 |
| --- src/markdown.c | |
| +++ src/markdown.c | |
| @@ -116,11 +116,11 @@ | |
| 116 | **********************/ |
| 117 | |
| 118 | /* markdown -- parses the input buffer and renders it into the output buffer */ |
| 119 | void markdown( |
| 120 | struct Blob *ob, |
| 121 | struct Blob *ib, |
| 122 | const struct mkd_renderer *rndr); |
| 123 | |
| 124 | |
| 125 | #endif /* INTERFACE */ |
| 126 | |
| @@ -2113,11 +2113,11 @@ | |
| 2113 | * REFERENCE PARSING * |
| 2114 | *********************/ |
| 2115 | |
| 2116 | /* is_ref -- returns whether a line is a reference or not */ |
| 2117 | static int is_ref( |
| 2118 | char *data, /* input text */ |
| 2119 | size_t beg, /* offset of the beginning of the line */ |
| 2120 | size_t end, /* offset of the end of the text */ |
| 2121 | size_t *last, /* last character of the link */ |
| 2122 | struct Blob *refs /* array of link references */ |
| 2123 | ){ |
| @@ -2240,15 +2240,15 @@ | |
| 2240 | * FOOTNOTE PARSING * |
| 2241 | *********************/ |
| 2242 | |
| 2243 | /* is_footnote -- returns whether a line is a footnote or not */ |
| 2244 | static int is_footnote( |
| 2245 | char *data, /* input text */ |
| 2246 | size_t beg, /* offset of the beginning of the line */ |
| 2247 | size_t end, /* offset of the end of the text */ |
| 2248 | size_t *last, /* last character of the link */ |
| 2249 | struct Blob * footnotes /* FIXME: struct render *rndr */ |
| 2250 | ){ |
| 2251 | size_t i = 0; |
| 2252 | size_t id_offset, id_end; |
| 2253 | size_t note_offset, note_end; |
| 2254 | size_t line_end; |
| @@ -2304,19 +2304,18 @@ | |
| 2304 | **********************/ |
| 2305 | |
| 2306 | /* markdown -- parses the input buffer and renders it into the output buffer */ |
| 2307 | void markdown( |
| 2308 | struct Blob *ob, /* output blob for rendered text */ |
| 2309 | struct Blob *ib, /* input blob in markdown */ |
| 2310 | const struct mkd_renderer *rndrer /* renderer descriptor (callbacks) */ |
| 2311 | ){ |
| 2312 | struct link_ref *lr; |
| 2313 | struct footnote *fn; |
| 2314 | size_t i, beg, end = 0; |
| 2315 | struct render rndr; |
| 2316 | char *ib_data; |
| 2317 | Blob text = BLOB_INITIALIZER; |
| 2318 | |
| 2319 | /* filling the render structure */ |
| 2320 | if( !rndrer ) return; |
| 2321 | rndr.make = *rndrer; |
| 2322 | rndr.nBlobCache = 0; |
| @@ -2339,31 +2338,30 @@ | |
| 2339 | if( rndr.make.image || rndr.make.link ) rndr.active_char['['] = char_link; |
| 2340 | rndr.active_char['<'] = char_langle_tag; |
| 2341 | rndr.active_char['\\'] = char_escape; |
| 2342 | rndr.active_char['&'] = char_entity; |
| 2343 | |
| 2344 | /* first pass: looking for references, copying everything else */ |
| 2345 | beg = 0; |
| 2346 | ib_data = blob_buffer(ib); |
| 2347 | while( beg<blob_size(ib) ){ /* iterating over lines */ |
| 2348 | if( is_ref(ib_data, beg, blob_size(ib), &end, &rndr.refs) ){ |
| 2349 | beg = end; |
| 2350 | }else if( is_footnote(ib_data, beg, blob_size(ib), &end, &rndr.footnotes) ){ |
| 2351 | /* FIXME: fossil_print("\nfootnote found at %i\n", beg); */ |
| 2352 | beg = end; |
| 2353 | }else{ /* skipping to the next line */ |
| 2354 | end = beg; |
| 2355 | while( end<blob_size(ib) && ib_data[end]!='\n' && ib_data[end]!='\r' ){ |
| 2356 | end += 1; |
| 2357 | } |
| 2358 | /* adding the line body if present */ |
| 2359 | if( end>beg ) blob_append(&text, ib_data + beg, end - beg); |
| 2360 | while( end<blob_size(ib) && (ib_data[end]=='\n' || ib_data[end]=='\r') ){ |
| 2361 | /* add one \n per newline */ |
| 2362 | if( ib_data[end]=='\n' |
| 2363 | || (end+1<blob_size(ib) && ib_data[end+1]!='\n') |
| 2364 | ){ |
| 2365 | blob_append_char(&text, '\n'); |
| 2366 | } |
| 2367 | end += 1; |
| 2368 | } |
| 2369 | beg = end; |
| 2370 |
| --- src/markdown.c | |
| +++ src/markdown.c | |
| @@ -116,11 +116,11 @@ | |
| 116 | **********************/ |
| 117 | |
| 118 | /* markdown -- parses the input buffer and renders it into the output buffer */ |
| 119 | void markdown( |
| 120 | struct Blob *ob, |
| 121 | const struct Blob *ib, |
| 122 | const struct mkd_renderer *rndr); |
| 123 | |
| 124 | |
| 125 | #endif /* INTERFACE */ |
| 126 | |
| @@ -2113,11 +2113,11 @@ | |
| 2113 | * REFERENCE PARSING * |
| 2114 | *********************/ |
| 2115 | |
| 2116 | /* is_ref -- returns whether a line is a reference or not */ |
| 2117 | static int is_ref( |
| 2118 | const char *data, /* input text */ |
| 2119 | size_t beg, /* offset of the beginning of the line */ |
| 2120 | size_t end, /* offset of the end of the text */ |
| 2121 | size_t *last, /* last character of the link */ |
| 2122 | struct Blob *refs /* array of link references */ |
| 2123 | ){ |
| @@ -2240,15 +2240,15 @@ | |
| 2240 | * FOOTNOTE PARSING * |
| 2241 | *********************/ |
| 2242 | |
| 2243 | /* is_footnote -- returns whether a line is a footnote or not */ |
| 2244 | static int is_footnote( |
| 2245 | const char *data, /* input text */ |
| 2246 | size_t beg, /* offset of the beginning of the line */ |
| 2247 | size_t end, /* offset of the end of the text */ |
| 2248 | size_t *last, /* last character of the link */ |
| 2249 | struct Blob * footnotes |
| 2250 | ){ |
| 2251 | size_t i = 0; |
| 2252 | size_t id_offset, id_end; |
| 2253 | size_t note_offset, note_end; |
| 2254 | size_t line_end; |
| @@ -2304,19 +2304,18 @@ | |
| 2304 | **********************/ |
| 2305 | |
| 2306 | /* markdown -- parses the input buffer and renders it into the output buffer */ |
| 2307 | void markdown( |
| 2308 | struct Blob *ob, /* output blob for rendered text */ |
| 2309 | const struct Blob *ib, /* input blob in markdown */ |
| 2310 | const struct mkd_renderer *rndrer /* renderer descriptor (callbacks) */ |
| 2311 | ){ |
| 2312 | struct link_ref *lr; |
| 2313 | struct footnote *fn; |
| 2314 | size_t i, beg, end = 0; |
| 2315 | struct render rndr; |
| 2316 | Blob text = BLOB_INITIALIZER; /* input after the first pass */ |
| 2317 | |
| 2318 | /* filling the render structure */ |
| 2319 | if( !rndrer ) return; |
| 2320 | rndr.make = *rndrer; |
| 2321 | rndr.nBlobCache = 0; |
| @@ -2339,31 +2338,30 @@ | |
| 2338 | if( rndr.make.image || rndr.make.link ) rndr.active_char['['] = char_link; |
| 2339 | rndr.active_char['<'] = char_langle_tag; |
| 2340 | rndr.active_char['\\'] = char_escape; |
| 2341 | rndr.active_char['&'] = char_entity; |
| 2342 | |
| 2343 | /* first pass: iterate over lines looking for references, |
| 2344 | * copying everything else into "text" */ |
| 2345 | beg = 0; |
| 2346 | for(const size_t size = blob_size(ib); beg<size ;){ |
| 2347 | const char* const data = blob_buffer(ib); |
| 2348 | if( is_ref(data, beg, size, &end, &rndr.refs) ){ |
| 2349 | beg = end; |
| 2350 | }else if( is_footnote(data, beg, size, &end, &rndr.footnotes) ){ |
| 2351 | /* FIXME: fossil_print("\nfootnote found at %i\n", beg); */ |
| 2352 | beg = end; |
| 2353 | }else{ /* skipping to the next line */ |
| 2354 | end = beg; |
| 2355 | while( end<size && data[end]!='\n' && data[end]!='\r' ){ |
| 2356 | end += 1; |
| 2357 | } |
| 2358 | /* adding the line body if present */ |
| 2359 | if( end>beg ) blob_append(&text, data + beg, end - beg); |
| 2360 | while( end<size && (data[end]=='\n' || data[end]=='\r') ){ |
| 2361 | /* add one \n per newline */ |
| 2362 | if( data[end]=='\n' || (end+1<size && data[end+1]!='\n') ){ |
| 2363 | blob_append_char(&text, '\n'); |
| 2364 | } |
| 2365 | end += 1; |
| 2366 | } |
| 2367 | beg = end; |
| 2368 |