词条 | Newline | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
释义 |
Newline (frequently called line ending, end of line (EOL), line feed, or line break) is a control character or sequence of control characters in a character encoding specification (e.g. ASCII or EBCDIC) that is used to signify the end of a line of text and the start of a new one. Text editors set this special character when pressing the {{key press|Enter}} key. When displaying (or printing) a text file, this control character causes the text editor to show the following characters in a new line. HistoryIn the mid-1800s, long before the advent of teleprinters and teletype machines, Morse code operators or telegraphists invented and used Morse code prosigns to encode white space text formatting in formal written text messages. In particular the Morse prosign represented by the concatenation of two literal textual Morse code "A" characters sent without the normal inter-character spacing is used in Morse code to encode and indicate a new line in a formal text message. Later in the age of modern teleprinters standardized character set control codes were developed to aid in white space text formatting. ASCII was developed simultaneously by the International Organization for Standardization (ISO) and the American Standards Association (ASA), the latter being the predecessor organization to American National Standards Institute (ANSI). During the period of 1963 to 1968, the ISO draft standards supported the use of either {{mono|CR}}+{{mono|LF}} or {{mono|LF}} alone as a newline, while the ASA drafts supported only {{mono|CR}}+{{mono|LF}}. The sequence {{mono|CR}}+{{mono|LF}} was commonly used on many early computer systems that had adopted Teletype machines—typically a Teletype Model 33 ASR—as a console device, because this sequence was required to position those printers at the start of a new line. The separation of newline into two functions concealed the fact that the print head could not return from the far right to the beginning of the next line in time to print the next character. Any character printed after a CR would often print as a smudge in the middle of the page while the print head was still moving the carriage back to the first position. "The solution was to make the newline two characters: CR to move the carriage to column one, and LF to move the paper up."[1] In fact, it was often necessary to send extra characters—extraneous CRs or NULs—which are ignored but give the print head time to move to the left margin. Many early video displays also required multiple character times to scroll the display. On such systems, applications had to talk directly to the Teletype machine and follow its conventions since the concept of device drivers hiding such hardware details from the application was not yet well developed. Therefore, text was routinely composed to satisfy the needs of Teletype machines. Most minicomputer systems from DEC used this convention. CP/M also used it in order to print on the same terminals that minicomputers used. From there MS-DOS (1981) adopted CP/M's {{mono|CR}}+{{mono|LF}} in order to be compatible, and this convention was inherited by Microsoft's later Windows operating system. The Multics operating system began development in 1964 and used {{mono|LF}} alone as its newline. Multics used a device driver to translate this character to whatever sequence a printer needed (including extra padding characters), and the single byte was more convenient for programming. What seems like a more obvious choice—{{mono|CR}}—was not used, as {{mono|CR}} provided the useful function of overprinting one line with another to create boldface and strikethrough effects. Perhaps more importantly, the use of {{mono|LF}} alone as a line terminator had already been incorporated into drafts of the eventual ISO/IEC 646 standard. Unix followed the Multics practice, and later Unix-like systems followed Unix. RepresentationThe concepts of line feed (LF) and carriage return (CR) are closely associated, and can be considered either separately or together. In the physical media of typewriters and printers, two axes of motion, "down" and "across", are needed to create a new line on the page. Although the design of a machine (typewriter or printer) must consider them separately, the abstract logic of software can combine them together as one event. This is why a newline in character encoding can be defined as Some character sets provide a separate newline character code. EBCDIC, for example, provides an NL character code in addition to the CR and LF codes. Unicode, in addition to providing the ASCII CR and LF control codes, also provides a "next line" (NEL) control code, as well as control codes for "line separator" and "paragraph separator" markers. Software applications and operating systems usually represent a newline with one or two control characters:
UnicodeThe Unicode standard defines a number of characters that conforming applications should recognize as line terminators:[6] {{mono| LF}}:{{mono| }} Line Feed, {{mono|U+000A}} {{mono| VT}}:{{mono| }} Vertical Tab, {{mono|U+000B}} {{mono| FF}}:{{mono| }} Form Feed, {{mono|U+000C}} {{mono| CR}}:{{mono| }} Carriage Return, {{mono|U+000D}} {{mono| CR}}+{{mono|LF}}: {{mono|CR}} ({{mono|U+000D}}) followed by {{mono|LF}} ({{mono|U+000A}}) {{mono| NEL}}:{{mono| }} Next Line, {{mono|U+0085}} {{mono| LS}}:{{mono| }} Line Separator, {{mono|U+2028}} {{mono| PS}}:{{mono| }} Paragraph Separator, {{mono|U+2029}} This may seem overly complicated compared to an approach such as converting all line terminators to a single character, for example {{mono|LF}}. However, Unicode was designed to preserve all information when converting a text file from any existing encoding to Unicode and back. Therefore, Unicode should contain characters included in existing encodings. {{mono|NEL}} is included in EBCDIC with code (0x15). {{mono|NEL}} is also a control character in the C1 control set.[7] As such, it is defined by ECMA 48,[8] and recognized by encodings compliant with ISO/IEC 2022 (which is equivalent to ECMA 35).[9] C1 control set is also compatible with ISO-8859-1.{{citation needed|date=October 2011}} The approach taken in the Unicode standard allows round-trip transformation to be information-preserving while still enabling applications to recognize all possible types of line terminators. Recognizing and using the newline codes greater than 0x7F (NEL, LS and PS) is not often done. They are multiple bytes in UTF-8, and the code for NEL has been used as the ellipsis ('…') character in Windows-1252. For instance:
The Unicode characters U+2424 (SYMBOL FOR NEWLINE, ), U+23CE (RETURN SYMBOL, ⏎), U+240D (SYMBOL FOR CARRIAGE RETURN, ␍) and U+240A (SYMBOL FOR LINE FEED, ␊) are intended for presenting a user-visible character to the reader of the document, and are thus not recognized themselves as a newline. Escape sequencesAn escape sequence is a combination of characters which represents no text; instead of being displayed (as text) it is supposed to be intercepted by the program and a special function is supposed to be performed. Escape sequences are also used to handle (set, search, replace, etc.) special characters.
In programming languagesTo facilitate the creation of portable programs, programming languages provide some abstractions to deal with the different types of newline sequences used in different environments. The C programming language provides the escape sequences {{mono|'\'}} (newline) and {{mono|'\\r'}} (carriage return). However, these are not required to be equivalent to the ASCII {{mono|LF}} and {{mono|CR}} control characters. The C standard only guarantees two things:
On Unix platforms, where C originated, the native newline sequence is ASCII {{mono|LF}} ({{mono|0x0A}}), so {{mono|'\'}} was simply defined to be that value. With the internal and external representation being identical, the translation performed in text mode is a no-op, and Unix has no notion of text mode or binary mode. This has caused many programmers who developed their software on Unix systems simply to ignore the distinction completely, resulting in code that is not portable to different platforms. The C library function {{mono|fgets()}} is best avoided in binary mode because any file not written with the Unix newline convention will be misread. Also, in text mode, any file not written with the system's native newline sequence (such as a file created on a Unix system, then copied to a Windows system) will be misread as well. Another common problem is the use of {{mono|'\'}} when communicating using an Internet protocol that mandates the use of ASCII {{mono|CR}}+{{mono|LF}} for ending lines. Writing {{mono|'\'}} to a text mode stream works correctly on Windows systems, but produces only {{mono|LF}} on Unix, and something completely different on more exotic systems. Using {{mono|"\\r\"}} in binary mode is slightly better. Many languages, such as C++, Perl,[12] and Haskell provide the same interpretation of {{mono|'\'}} as C. C++ has an alternative I/O model where the manipulator {{mono|std::endl}} can be used to output a newline (and flushes the stream buffer). Java, PHP,[13] and Python[14] provide the {{mono|'\\r\'}} sequence (for ASCII {{mono|CR}}+{{mono|LF}}). In contrast to C, these are guaranteed to represent the values {{mono|U+000D}} and {{mono|U+000A}}, respectively. The Java I/O libraries do not transparently translate these into platform-dependent newline sequences on input or output. Instead, they provide functions for writing a full line that automatically add the native newline sequence, and functions for reading lines that accept any of {{mono|CR}}, {{mono|LF}}, or {{mono|CR}}+{{mono|LF}} as a line terminator (see {{mono|BufferedReader.readLine()}}). The {{mono|System.lineSeparator()}} method can be used to retrieve the underlying line separator. Example: Python permits "Universal Newline Support" when opening a file for reading, when importing modules, and when executing a file.[15] Some languages have created special variables, constants, and subroutines to facilitate newlines during program execution. In some languages such as PHP and Perl, double quotes are required to perform escape substitution for all escape sequences, including {{mono|'\'}} and {{mono|'\\r'}}. In PHP, to avoid portability problems, newline sequences should be issued using the PHP_EOL constant.[16] Example in C#: Issues with different newline formatsEven though the control characters are unambiguously defined in the corresponding character encoding table used by a text file, there still is an issue: there are different conventions to set and display a line break. To denote a single line break, Unix programs use The different newline conventions cause text files that have been transferred between systems of different types to be displayed incorrectly. Text in files created with programs which are common on Unix-like or classic Mac OS, appear as a single long line on most programs common to MS-DOS and Microsoft Windows because these do not display a single Conversely, when viewing a file originating from a Windows computer on a Unix-like system, the extra {{mono|CR}} may be displayed as a second line break, as {{mono|^M}}, or as {{mono| Furthermore, programs other than text editors may not accept a file, e.g. some configuration file, encoded using the foreign newline convention, as a valid file. The problem can be hard to spot because some programs handle the foreign newlines properly while others do not. For example, a compiler may fail with obscure syntax errors even though the source file looks correct when displayed on the console or in an editor. On a Unix-like system, the command {{mono|cat -v myfile.txt}} will send the file to stdout (normally the terminal) and make the {{mono|^M}} visible, which can be useful for debugging. Modern text editors generally recognize all flavours of {{mono|CR}}+{{mono|LF}} newlines and allow users to convert between the different standards. Web browsers are usually also capable of displaying text files and websites which use different types of newlines. Even if a program supports different newline conventions, these features are often not sufficiently labeled, described, or documented. Typically a menu or combo-box enumerating different newline conventions will be displayed to users without an indication if the selection will re-interpret, temporarily convert, or permanently convert the newlines. Some programs will implicitly convert on open, copy, paste, or save—often inconsistently. Most textual Internet protocols (including HTTP, SMTP, FTP, IRC, and many others) mandate the use of ASCII {{mono|CR}}+{{mono|LF}} ({{mono|'\\r\'}}, {{mono|0x0D 0x0A}}) on the protocol level, but recommend that tolerant applications recognize lone {{mono|LF}} ({{mono|'\'}}, {{mono|0x0A}}) as well. Despite the dictated standard, many applications erroneously use the C newline escape sequence {{mono|'\'}} ({{mono|LF}}) instead of the correct combination of carriage return escape and newline escape sequences {{mono|'\\r\'}} ({{mono|CR}}+{{mono|LF}}) (see section Newline in programming languages above). This accidental use of the wrong escape sequences leads to problems when trying to communicate with systems adhering to the stricter interpretation of the standards instead of the suggested tolerant interpretation. One such intolerant system is the qmail mail transfer agent that actively refuses to accept messages from systems that send bare {{mono|LF}} instead of the required {{mono|CR}}+{{mono|LF}}.[17] The standard Internet Message Format[18] for eMail states: CR and LF MUST only occur together as CRLF; they MUST NOT appear independently in the body. The File Transfer Protocol can automatically convert newlines in files being transferred between systems with different newline representations when the transfer is done in "ASCII mode". However, transferring binary files in this mode usually has disastrous results: any occurrence of the newline byte sequence—which does not have line terminator semantics in this context, but is just part of a normal sequence of bytes—will be translated to whatever newline representation the other system uses, effectively corrupting the file. FTP clients often employ some heuristics (for example, inspection of filename extensions) to automatically select either binary or ASCII mode, but in the end it is up to users to make sure their files are transferred in the correct mode. If there is any doubt as to the correct mode, binary mode should be used, as then no files will be altered by FTP, though they may display incorrectly.[19] Conversion between newline formats{{Howto|date=June 2010}}Text editors are often used for converting a text file between different newline formats; most modern editors can read and write files using at least the different ASCII {{mono|CR}}/{{mono|LF}} conventions. The standard Windows editor Notepad is not one of them (although WordPad and the MS-DOS Editor are). Editors are often unsuitable for converting larger files. For larger files (on Windows NT/2000/XP) the following command is often used: On many Unix systems, the {{mono|dos2unix}} (sometimes named {{mono|fromdos}} or {{mono|d2u}}) and {{mono|unix2dos}} (sometimes named {{mono|todos}} or {{mono|u2d}}) utilities are used to translate between ASCII {{mono|CR}}+{{mono|LF}} (DOS/Windows) and {{mono|LF}} (Unix) newlines. Different versions of these commands vary slightly in their syntax. However, the {{mono|tr}} command is available on virtually every Unix-like system and can be used to perform arbitrary replacement operations on single characters. A DOS/Windows text file can be converted to Unix format by simply removing all ASCII {{mono|CR}} characters with or, if the text has only {{mono|CR}} newlines, by converting all {{mono|CR}} newlines to {{mono|LF}} with The same tasks are sometimes performed with awk, sed, or in Perl if the platform has a Perl interpreter: To identify what type of line breaks a text file contains, the file command can be used. Moreover, the editor Vim can be convenient to make a file compatible with the Windows notepad text editor. For example: within vim The following grep commands echo the filename (in this case myfile.txt) to the command line if the file is of the specified style: For systems with egrep (extended grep) such as Debian (Linux) based systems and many other Unix systems, these commands can be used: The above grep commands work under Unix systems or in Cygwin under Windows. Note that these commands make some assumptions about the kinds of files that exist on the system (specifically it's assuming only Unix and DOS-style files—no Mac OS 9-style files). This technique is often combined with find to list files recursively. For instance, the following command checks all "regular files" (e.g. it will exclude directories, symbolic links, etc.) to find all Unix-style files in a directory tree, starting from the current directory (.), and saves the results in file unix_files.txt, overwriting it if the file already exists: This example will find C files and convert them to LF style line endings: The file command also detects the type of EOL used: Other tools permit the user to visualise the EOL characters: dos2unix, unix2dos, mac2unix, unix2mac, mac2dos, dos2mac can perform conversions. The flip[20] command is often used. InterpretationTwo ways to view newlines, both of which are self-consistent, are that newlines either separate lines or that they terminate lines. If a newline is considered a separator, there will be no newline after the last line of a file. Some programs have problems processing the last line of a file if it is not terminated by a newline. On the other hand, programs that expect newline to be used as a separator will interpret a final newline as starting a new (empty) line. Conversely, if a newline is considered a terminator, all text lines including the last are expected to be terminated by a newline. If the final character sequence in a text file is not a newline, the final line of the file may be considered to be an improper or incomplete text line, or the file may be considered to be improperly truncated. In text intended primarily to be read by humans using software which implements the word wrap feature, a newline character typically only needs to be stored if a line break is required independent of whether the next word would fit on the same line, such as between paragraphs and in vertical lists. Therefore, in the logic of word processing and most text editors, newline is used as a paragraph break and is known as a "hard return", in contrast to "soft returns" which are dynamically created to implement word wrapping and are changeable with each display instance. In many applications a separate control character called "manual line break" exists for forcing line breaks inside a single paragraph. The glyph for the control character for a hard return is usually a pilcrow (¶), and for the manual line break is usually a carriage return arrow (↵). Reverse and partial line feeds{{refimprove|date=February 2016}}{{mono|RI}}, (U+008D REVERSE LINE FEED,[21] ISO/IEC 6429 8D, decimal 141) is used to move the printing position back one line (by reverse feeding the paper, or by moving a display cursor up one line) so that other characters may be printed over existing text. This may be done to make them bolder, or to add underlines, strike-throughs or other characters such as diacritics.Similarly, {{mono|PLD}} (U+008B PARTIAL LINE FORWARD, decimal 139) and {{mono|PLU}} (U+008C PARTIAL LINE BACKWARD, decimal 140) can be used to advance or reverse the text printing position by some fraction of the vertical line spacing (typically, half). These can be used in combination for subscripts (by advancing and then reversing) and superscripts (by reversing and then advancing), and may also be useful for printing diacritics. See also
References1. ^{{cite book |last=Qualline |first=Steve |title=Vi Improved - Vim |year=2001 |publisher=Sams |isbn=9780735710016 |page=120 |url=ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf}} 2. ^{{cite web|url=http://www.bluesock.org/~willg/dev/ascii.html|title=ASCII Chart}} 3. ^{{cite book |last1=Bray |first1=Andrew C. |last2=Dickens |first2=Adrian C. |last3=Holmes |first3=Mark A. |title=The Advanced User Guide for the BBC Microcomputer |isbn=978-0946827008 |pages=103,104 |url=http://stardot.org.uk/mirrors/www.bbcdocs.com/filebase/essentials/BBC%20Microcomputer%20Advanced%20User%20Guide.pdf |accessdate=30 January 2019}} 4. ^{{cite web |title=RISC OS 3 Programmers' Reference Manual |url=http://www.riscos.com/support/developers/prm/charoutput.html#66036 |accessdate=18 July 2018}} 5. ^IBM System/360 Reference Data Card, Publication GX20-1703, IBM Data Processing Division, White Plains, NY 6. ^{{cite web|url=https://www.unicode.org/reports/tr14/tr14-32.html|title=UAX #14: Unicode Line Breaking Algorithm|website=www.unicode.org}} 7. ^{{cite web|url=https://www.itscj.ipsj.or.jp/iso-ir/077.pdf|title=C1 Control Character Set of ISO 6429|date=October 1, 1983}} 8. ^{{cite web|url=http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf|title=Control Functions for Coded Character Sets|date=June 1991}} 9. ^{{cite web|url=http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-035.pdf|title=Character Code Structure and Extension Techniques, 6th edition|date=December 1994}} 10. ^{{cite web |title=ECMAScript Language Specification 5th edition |date= December 2009 |url= http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf |page= 15 |publisher= ECMA International |accessdate= 4 April 2010}} 11. ^{{cite web|url=http://yaml.org/spec/1.2/spec.html|title=YAML Ain’t Markup Language (YAML™) Version 1.2|website=yaml.org}} 12. ^{{cite web|url=http://perldoc.perl.org/functions/binmode.html|title=binmode - perldoc.perl.org|website=perldoc.perl.org}} 13. ^{{cite web|url=http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double|title=PHP: Strings - Manual|website=www.php.net}} 14. ^{{cite web|url=https://docs.python.org/3.0/reference/lexical_analysis.html#id9|title=Lexical analysis — Python v3.0.1 documentation|website=docs.python.org}} 15. ^{{cite web|url=https://www.python.org/doc/2.3.5/whatsnew/node7.html|title=What's new in Python 2.3|publisher=}} 16. ^{{cite web|url=http://www.php.net/manual/en/reserved.constants.php|title=PHP: Predefined Constants - Manual|website=www.php.net}} 17. ^{{cite web|url=http://cr.yp.to/docs/smtplf.html|title=cr.yp.to|publisher=}} 18. ^{{cite web|title=RFC 2822 - Internet Message Format|url=https://tools.ietf.org/html/rfc2822|website=The Internet Engineering Task Force}} 19. ^{{cite web|url=https://www.cs.odu.edu/~zeil/cs252/s15/Public/ftp/|title=File Transfer|quote=When in doubt, transfer in binary mode.}} 20. ^{{cite web |title=ASCII text converstion between UNIX, Macintosh, MS-DOS |url=http://ccrma-www.stanford.edu/~craig/utility/flip/ |archiveurl=https://web.archive.org/web/20090209015201/http://ccrma-www.stanford.edu/~craig/utility/flip/ |archivedate=2009-02-09}} 21. ^{{cite web|url=https://www.unicode.org/charts/PDF/U0080.pdf|title=C1 Controls and Latin-1 Supplement|website=unicode.org|accessdate=13 February 2016}} External links
2 : Control characters|Whitespace |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。