TextPosition

Exports textpos_t which describes position in text as column and line number.  A text is a big string which contains special formatting characters.  The only formatting character which is supported by any text function handling UTF-8 encoded strings is the ‘\n’ newline characters which marks the end of a text line.

Summary
TextPositionExports textpos_t which describes position in text as column and line number.
CopyrightThis program is free software.
Files
C-kern/api/string/textpos.hHeader file TextPosition.
C-kern/string/textpos.cImplementation file TextPosition impl.
Types
struct textpos_tExport textpos_t into global namespace.
Functions
test
unittest_string_textposTest textpos_t functionality.
textpos_tDescribes position in text as column and line number.
columnColumn number of the last read character.
lineLine number of the next unread character.
prevlastcolumnColumn number of the last character on the previous line.
lifetime
textpos_INITStatic initializer.
textpos_FREEStatic initializer.
init_textposSets column and line numbers to arbitrary values.
free_textposSets pos to textpos_FREE.
query
column_textposReturns the current column number beginning from 0.
line_textposReturns the current line number beginning from 1.
prevlastcolumn_textposReturns the column number of the last character of the previous line (number).
change
addcolumn_textposAdds increment to the column number.
incrcolumn_textposIncrements the column number.
incrline_textposIncrements the line number.
inline implementation
textpos_t
addcolumn_textposImplements textpos_t.addcolumn_textpos.
column_textposImplements textpos_t.column_textpos.
free_textposImplements textpos_t.free_textpos.
init_textposImplements textpos_t.init_textpos.
line_textposImplements textpos_t.line_textpos.
incrcolumn_textposImplements textpos_t.incrcolumn_textpos.
incrline_textposImplements textpos_t.incrline_textpos.
prevlastcolumn_textposImplements textpos_t.prevlastcolumn_textpos.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2013 Jörg Seebohn

Files

C-kern/api/string/textpos.h

Header file TextPosition.

C-kern/string/textpos.c

Implementation file TextPosition impl.

Types

struct textpos_t

typedef struct textpos_t textpos_t

Export textpos_t into global namespace.

Functions

Summary

test

unittest_string_textpos

int unittest_string_textpos(void)

Test textpos_t functionality.

textpos_t

struct textpos_t

Describes position in text as column and line number.  The first line has the line number 1 and the beginning of the text or the beginning of a new line has the column number 0.  After reading a newline ‘\n’ the column number is reset to 0 and the line number is incremented.  Reading another character increments the column number but does not change the line number.  A text reader should call the functions

Summary
columnColumn number of the last read character.
lineLine number of the next unread character.
prevlastcolumnColumn number of the last character on the previous line.
lifetime
textpos_INITStatic initializer.
textpos_FREEStatic initializer.
init_textposSets column and line numbers to arbitrary values.
free_textposSets pos to textpos_FREE.
query
column_textposReturns the current column number beginning from 0.
line_textposReturns the current line number beginning from 1.
prevlastcolumn_textposReturns the column number of the last character of the previous line (number).
change
addcolumn_textposAdds increment to the column number.
incrcolumn_textposIncrements the column number.
incrline_textposIncrements the line number.

column

size_t column

Column number of the last read character.  This value is initialized to 0.  The reading of a newline character ‘\n’ resets this value to 0 to reflect the beginning of a line.

line

size_t line

Line number of the next unread character.  This value is initialized to 1.  It is incremented every time a newline character ‘\n’ is encountered.

prevlastcolumn

size_t prevlastcolumn

Column number of the last character on the previous line.  This value is a copy of column before it is set to 0 in function incrline_textpos.

lifetime

textpos_INIT

#define textpos_INIT { 0, 1, 0 }

Static initializer.  Initializes position to start of text.

textpos_FREE

#define textpos_FREE { 0, 0, 0 }

Static initializer.

init_textpos

void init_textpos(/*out*/textpos_t *txtpos,
size_t colnr,
size_t linenr)

Sets column and line numbers to arbitrary values.

free_textpos

void free_textpos(textpos_t *txtpos)

Sets pos to textpos_FREE.

query

column_textpos

size_t column_textpos(const textpos_t *txtpos)

Returns the current column number beginning from 0.

line_textpos

size_t line_textpos(const textpos_t *txtpos)

Returns the current line number beginning from 1.

prevlastcolumn_textpos

size_t prevlastcolumn_textpos(const textpos_t *txtpos)

Returns the column number of the last character of the previous line (number).

change

addcolumn_textpos

size_t addcolumn_textpos(textpos_t *txtpos,
size_t increment)

Adds increment to the column number.  The incremented column number is returned.

incrcolumn_textpos

void incrcolumn_textpos(textpos_t *txtpos)

Increments the column number.

incrline_textpos

void incrline_textpos(textpos_t *txtpos)

Increments the line number.

textpos_t

addcolumn_textpos

#define addcolumn_textpos(txtpos,
increment) ((txtpos)->column += (increment))

Implements textpos_t.addcolumn_textpos.

column_textpos

#define column_textpos(txtpos) ((txtpos)->column)

Implements textpos_t.column_textpos.

free_textpos

#define free_textpos(txtpos) ((void)(*(txtpos) = (textpos_t)textpos_FREE))

Implements textpos_t.free_textpos.

init_textpos

#define init_textpos(
   txtpos,
   colnr,
   linenr
) ((void)(*(txtpos) = (textpos_t) { colnr, linenr, 0 }))

Implements textpos_t.init_textpos.

line_textpos

#define line_textpos(txtpos) ((txtpos)->line)

Implements textpos_t.line_textpos.

incrcolumn_textpos

#define incrcolumn_textpos(txtpos) ((void)(++(txtpos)->column))

Implements textpos_t.incrcolumn_textpos.

incrline_textpos

#define incrline_textpos(
   txtpos
) do { textpos_t * _tpos = (txtpos) ; _tpos->prevlastcolumn = _tpos->column ; _tpos->column = 0 ; ++ _tpos->line ; } while (0)

Implements textpos_t.incrline_textpos.

prevlastcolumn_textpos

#define prevlastcolumn_textpos(txtpos) ((txtpos)->prevlastcolumn)

Implements textpos_t.prevlastcolumn_textpos.

struct textpos_t
Describes position in text as column and line number.
Exports textpos_t which describes position in text as column and line number.
Implements TextPosition.
typedef struct textpos_t textpos_t
Export textpos_t into global namespace.
int unittest_string_textpos(void)
Test textpos_t functionality.
size_t column
Column number of the last read character.
size_t line
Line number of the next unread character.
size_t prevlastcolumn
Column number of the last character on the previous line.
#define textpos_INIT { 0, 1, 0 }
Static initializer.
#define textpos_FREE { 0, 0, 0 }
Static initializer.
void init_textpos(/*out*/textpos_t *txtpos,
size_t colnr,
size_t linenr)
Sets column and line numbers to arbitrary values.
void free_textpos(textpos_t *txtpos)
Sets pos to textpos_FREE.
size_t column_textpos(const textpos_t *txtpos)
Returns the current column number beginning from 0.
size_t line_textpos(const textpos_t *txtpos)
Returns the current line number beginning from 1.
size_t prevlastcolumn_textpos(const textpos_t *txtpos)
Returns the column number of the last character of the previous line (number).
size_t addcolumn_textpos(textpos_t *txtpos,
size_t increment)
Adds increment to the column number.
void incrcolumn_textpos(textpos_t *txtpos)
Increments the column number.
void incrline_textpos(textpos_t *txtpos)
Increments the line number.
#define addcolumn_textpos(txtpos,
increment) ((txtpos)->column += (increment))
Implements textpos_t.addcolumn_textpos.
#define column_textpos(txtpos) ((txtpos)->column)
Implements textpos_t.column_textpos.
#define free_textpos(txtpos) ((void)(*(txtpos) = (textpos_t)textpos_FREE))
Implements textpos_t.free_textpos.
#define init_textpos(
   txtpos,
   colnr,
   linenr
) ((void)(*(txtpos) = (textpos_t) { colnr, linenr, 0 }))
Implements textpos_t.init_textpos.
#define line_textpos(txtpos) ((txtpos)->line)
Implements textpos_t.line_textpos.
#define incrcolumn_textpos(txtpos) ((void)(++(txtpos)->column))
Implements textpos_t.incrcolumn_textpos.
#define incrline_textpos(
   txtpos
) do { textpos_t * _tpos = (txtpos) ; _tpos->prevlastcolumn = _tpos->column ; _tpos->column = 0 ; ++ _tpos->line ; } while (0)
Implements textpos_t.incrline_textpos.
#define prevlastcolumn_textpos(txtpos) ((txtpos)->prevlastcolumn)
Implements textpos_t.prevlastcolumn_textpos.
Close