Changeset 7ba2ad4


Ignore:
Timestamp:
Sep 18, 2010, 3:17:27 PM (14 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.10, release-1.7, release-1.8, release-1.9
Children:
b3b1b05
Parents:
4cf7b1b
git-author:
David Benjamin <davidben@mit.edu> (07/31/10 20:30:49)
git-committer:
David Benjamin <davidben@mit.edu> (09/18/10 15:17:27)
Message:
Implement owl_fmtext_line_extents and add test case
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • fmtext.c

    rf7456bc r7ba2ad4  
    597597}
    598598
     599/* Searches for line 'lineno' in 'f'. The returned range, [start,
     600 * end), forms a half-open interval for the extent of the line. */
     601void owl_fmtext_line_extents(const owl_fmtext *f, int lineno, int *o_start, int *o_end)
     602{
     603  int start, end;
     604  char *newline;
     605  for (start = 0; lineno > 0 && start < f->textlen; start++) {
     606    if (f->textbuff[start] == '\n')
     607      lineno--;
     608  }
     609  newline = strchr(f->textbuff + start, '\n');
     610  /* Include the newline, if it is there. */
     611  end = newline ? newline - f->textbuff + 1 : f->textlen;
     612  if (o_start) *o_start = start;
     613  if (o_end) *o_end = end;
     614}
     615
    599616const char *owl_fmtext_get_text(const owl_fmtext *f)
    600617{
  • tester.c

    rdc9665a r7ba2ad4  
    434434int owl_fmtext_regtest(void) {
    435435  int numfailed = 0;
     436  int start, end;
    436437  owl_fmtext fm1;
    437438  owl_fmtext fm2;
     
    557558  owl_regex_cleanup(&re);
    558559
     560  /* Test owl_fmtext_line_extents. */
     561  owl_fmtext_clear(&fm1);
     562  owl_fmtext_append_normal(&fm1, "123\n456\n789");
     563  owl_fmtext_line_extents(&fm1, 1, &start, &end);
     564  FAIL_UNLESS("line contents",
     565              !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start));
     566  owl_fmtext_line_extents(&fm1, 2, &start, &end);
     567  FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1));
     568
    559569  owl_fmtext_cleanup(&fm1);
    560570  owl_fmtext_cleanup(&fm2);
Note: See TracChangeset for help on using the changeset viewer.