4

Why am I asking?

I am given the severe task of re-implementing the text box GUI widget AND the type writer, so this is the origin of this question.


Current approach of mine

Currently in my project, the new line character \n holds position coordinates of an eventual first character on the next line (column1, line+1). However, I am afraid that this isn't the standard understanding of the newline position.


Question

Is the new line character \n part of the same line as the previous character, is it part of the following line (as it is in my current approach) or neither of these ?


Does it make a difference?

I am experiencing complications when erasing the new line with the backspace, because I have to implement a specific condition for this event, but I am trying to avoid this (or it could be avoided by changing the way I treat the new line character), because from all these conditions it looks like I am trying to invent AI or something.

Chad
  • 1,750
  • 2
  • 16
  • 32
Imobilis
  • 1,475
  • 8
  • 29
  • is the project open source, by any chance? – n611x007 Aug 25 '15 at 08:17
  • 1
    @naxa Yes it will be published open-source with GPL license. – Imobilis Aug 25 '15 at 09:23
  • 1
    It is built onto a platform that allows only the basic drawing functions (putpixel, lineto and setpen) as well as access to keyboard and mouse. The platform doesn't support 2D (nor 3D) hardware acceleration (as it is SDL1-based) but it is designed to be heavily optimized and stands on 166 fps on autotyping (auto textbox filling). It can be ported for use with CAIRO (for instance) as it uses only the most basic interactions as I said. – Imobilis Aug 25 '15 at 09:25
  • 1
    Despite these restrictions and the heavy optimizations it overreaches the standard textbox (from .NET env and even GNOME) regarding the power and aptness. (digital scrolling, layered pixbuff etc - confirmed) – Imobilis Aug 25 '15 at 09:27
  • 1
    Hey, sounds impressive! If you could hyperlink it here by the time when the repository goes public it would be great. – n611x007 Aug 25 '15 at 09:34
  • 1
    It is truly impressive, but I started it generally because there are no fully-functional textboxes coded to work on the platform. Therefore I don't see why anyone would prefer to use these (despite powerful, xplatform, customizable) but the standard GNOME textboxes. I agree they have some downsides (like the phantom selection) but they aren't that important (at least for common use) – Imobilis Aug 25 '15 at 09:42

2 Answers2

7

In general, the newline \n is the part of the current line, and when interpreted, emits a visual representation of the end of a line and moves the next character (onwards) to be printed on the next line.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    Thank you. And I will keep this in mind. It is very important for one to know this when doing what I do. Good that you understood the question. – Imobilis Aug 24 '15 at 18:51
1

7.21.2 Streams "A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. Whether the last line requires a terminating new-line character is implementation-defined. ..."

This specification likely meets OP's question the best. The new-line is part of the line that precedes it.


Other C references detailing some aspects of '\n'.

With a source code file the new-line is consistently treated as a terminating end-of-line

5.1.1.2 Translation phases: "... (introducing new-line characters for end-of-line indicators) ..."

6.4.9 Comments: "... the characters // ... and to find the terminating new-line character."

6.10.3 Macro replacement: "... until the new-line character that terminates the #define preprocessing directive. ..."

6.10 Preprocessing directives: " A new-line character ends the preprocessing directive ..."

6.10.4 Line control: "The line number of the current source line is one greater than the number of new-line characters read ..."

How a C program handles a '\n' is specified as

7.4.1.10 The isspace function: " ... standard white-space characters are ... new-line ('\n') ... "

5.2.2 Character display semantics "\n (new line) Moves the active position to the initial position of the next line."

--

To count the number of lines

unsigned long long file_line_count(FILE *inf) {
  unsigned long long line_count = 0;
  int ch;
  int previous = '\n'; 
  while((ch = fgetc(inf)) != EOF) {
    if(previous == '\n') {
      line_count++;
    }
    previous = ch;
  }
  return line_count;
}
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • could you hyperlink which version[s] are you quoting? – n611x007 Aug 24 '15 at 20:29
  • 1
    @naxa See http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents and http://www.iso.org/iso/catalogue_detail.htm?csnumber=29237 and http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf – chux - Reinstate Monica Aug 24 '15 at 21:01