Changeset f7456bc


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:
dc9665a
Parents:
5b68c05
git-author:
David Benjamin <davidben@mit.edu> (07/31/10 19:09:41)
git-committer:
David Benjamin <davidben@mit.edu> (09/18/10 15:17:27)
Message:
Add owl_fmtext_line_number and test.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • fmtext.c

    r449c682 rf7456bc  
    580580}
    581581
     582/* Returns the line number, starting at 0, of the character which
     583 * contains the byte at 'offset'. Note that a trailing newline is part
     584 * of the line it ends. Also, while a trailing line of formatting
     585 * characters does not contribute to owl_fmtext_num_lines, those
     586 * characters are considered on a new line. */
     587int owl_fmtext_line_number(const owl_fmtext *f, int offset)
     588{
     589  int i, lineno = 0;
     590  if (offset >= f->textlen)
     591    offset = f->textlen - 1;
     592  for (i = 0; i < offset; i++) {
     593    if (f->textbuff[i] == '\n')
     594      lineno++;
     595  }
     596  return lineno;
     597}
     598
    582599const char *owl_fmtext_get_text(const owl_fmtext *f)
    583600{
  • tester.c

    re5c9d3de rf7456bc  
    436436  owl_fmtext fm1;
    437437  owl_fmtext fm2;
     438  owl_regex re;
    438439  char *str;
    439440
     
    527528  owl_free(str);
    528529
     530  /* Test owl_fmtext_line_number. */
     531  owl_fmtext_clear(&fm1);
     532  owl_fmtext_append_normal(&fm1, "123\n456\n");
     533  owl_fmtext_append_bold(&fm1, "");
     534  FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0));
     535  FAIL_UNLESS("trailing formatting characters part of false line",
     536              2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1)));
     537  owl_regex_create_quoted(&re, "456");
     538  FAIL_UNLESS("correctly find second line (line 1)",
     539              1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0)));
     540  owl_regex_cleanup(&re);
     541
    529542  owl_fmtext_cleanup(&fm1);
    530543  owl_fmtext_cleanup(&fm2);
Note: See TracChangeset for help on using the changeset viewer.