HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, leaning toothpick syndrome (LTS) is the situation in which a quoted expression becomes unreadable because it contains a large number of
escape character In computing and telecommunication, an escape character is a character (computing), character that invokes an alternative interpretation on the following characters in a character sequence. An escape character is a particular case of metacharac ...
s, usually
backslash The backslash is a typographical mark used mainly in computing and mathematics. It is the mirror image of the common slash . It is a relatively recent mark, first documented in the 1930s. History , efforts to identify either the origin of ...
es ("\"), to avoid
delimiter collision A delimiter is a sequence of one or more characters for specifying the boundary between separate, independent regions in plain text, mathematical expressions or other data streams. An example of a delimiter is the comma character, which acts as ...
. The official
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
documentation introduced the term to wider usage; there, the phrase is used to describe
regular expression A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" ...
s that match
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, ...
-style paths, in which the elements are separated by slashes /. The slash is also used as the default regular expression delimiter, so to be used literally in the expression, it must be escaped with a ''back''slash \, leading to frequent escaped slashes represented as \/. If doubled, as in URLs, this yields \/\/ for an escaped //. A similar phenomenon occurs for
DOS DOS is shorthand for the MS-DOS and IBM PC DOS family of operating systems. DOS may also refer to: Computing * Data over signalling (DoS), multiplexing data onto a signalling channel * Denial-of-service attack (DoS), an attack on a communicat ...
/
Windows Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for se ...
paths, where the backslash is used as a path separator, requiring a doubled backslash \\ – this can then be ''re''-escaped for a regular expression inside an escaped string, requiring \\\\ to match a single backslash. In extreme cases, such as a regular expression in an escaped string, matching a
Uniform Naming Convention A path is a string of characters used to uniquely identify a location in a directory structure. It is composed by following the directory tree hierarchy in which components, separated by a delimiting character, represent each directory. The de ...
path (which begins \\) requires 8 backslashes \\\\\\\\ due to 2 backslashes each being double-escaped. LTS appears in many programming languages and in many situations, including in patterns that match
Uniform Resource Identifier A Uniform Resource Identifier (URI) is a unique sequence of characters that identifies a logical or physical resource used by web technologies. URIs may be used to identify anything, including real-world objects, such as people and places, conc ...
s (URIs) and in programs that output quoted text. Many quines fall into the latter category.


Pattern example

Consider the following Perl regular expression intended to match URIs that identify files under the pub directory of an FTP site:
m/ftp:\/\/ \/\/pub\//
Perl, like
sed sed ("stream editor") is a Unix utility that parses and transforms text, using a simple, compact programming language. It was developed from 1973 to 1974 by Lee E. McMahon of Bell Labs, and is available today for most operating systems. sed w ...
before it, solves this problem by allowing many other characters to be delimiters for a regular expression. For example, the following three examples are equivalent to the expression given above:
m
m#ftp:// //pub/#
m!ftp:// //pub/!
Or this common translation to convert backslashes to forward slashes:
tr/\\/\//
may be easier to understand when written like this:
tr


Quoted-text example

A Perl program to print an HTML link tag, where the URL and link text are stored in variables $url and $text respectively, might look like this. Notice the use of backslashes to escape the quoted double-quote characters: print "$text"; Using single quotes to delimit the string is not feasible, as Perl does not expand variables inside single-quoted strings. The code below, for example, would ''not'' work as intended: print '$text' Using the printf function is a viable solution in many languages (Perl, C, PHP): printf('%s', $url, $text); The qq operator in Perl allows for any delimiter: print qq; print qq, $text, ; print qq($text);
Here document In computing, a here document (here-document, here-text, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also ...
s are especially well suited for multi-line strings; however, Perl here documents hadn't allowed for proper
indentation __FORCETOC__ In the written form of many languages, an indentation or indent is an empty space at the beginning of a line to signal the start of a new paragraph. Many computer languages have adopted this technique to designate "paragraphs" or o ...
before v5.26. This example shows the Perl syntax: print <$text HERE_IT_ENDS


Other languages


C#

The C# programming language handles LTS by the use of the @ symbol at the start of string literals, before the initial quotation marks, e.g. string filePath = @"C:\Foo\Bar.txt"; rather than otherwise requiring: string filePath = "C:\\Foo\\Bar.txt";


C++

The
C++11 C11, C.XI, C-11 or C.11 may refer to: Transport * C-11 Fleetster, a 1920s American light transport aircraft for use of the United States Assistant Secretary of War * Fokker C.XI, a 1935 Dutch reconnaissance seaplane * LET C-11, a license-build ...
standard adds raw strings: std::string filePath = R"(C:\Foo\Bar.txt)"; If the string contains the characters )", an optional delimiter can be used, such as d in the following example: std::regex re;


Go

Go indicates that a string is raw by using the
backtick The backtick is a typographical mark used mainly in computing. It is also known as backquote, grave, or grave accent. The character was designed for typewriters to add a grave accent to a (lower-case) base letter, by overtyping it atop that le ...
as a delimiter: s := `C:\Foo\Bar.txt` Raw strings may contain any character except backticks; there is no escape code for a backtick in a raw string. Raw strings may also span multiple lines, as in this example, where the strings s and t are equivalent: s := `A string that spans multiple lines.` t := "A string that\nspans multiple\nlines."


Python

Python has a similar construct using r: filePath = r"C:\Foo\Bar.txt" One can also use them together with triple quotes: example = r"""First line : "C:\Foo\Bar.txt" Second line : nothing"""


Ruby

Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
uses single quote to indicate raw string: filePath = 'C:\Foo\Bar.txt' It also has regex percent literals with choice of delimiter like Perl: %r %r#ftp:// //pub/# %r!ftp:// //pub/!


Rust

Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO( ...
uses a variant of the r prefix:raw byte string literals
at rust-lang.org. "\x52"; // R r"\x52"; // \x52 r#""foo""#; // "foo" r##"foo #"# bar"##; // foo #"# bar The literal starts with r followed by any number of #, followed by one ". Further " contained in the literal are considered part of the literal, unless followed by at least as many # as used after the opening r.


Scala

Scala allows usage of triple quotes in order to prevent escaping confusion: val filePath = """C:\Foo\Bar.txt""" val pubPattern = """ftp:// //pub/"""r The triple quotes also allow for multiline strings, as shown here: val text = """First line, second line."""


Sed

Sed sed ("stream editor") is a Unix utility that parses and transforms text, using a simple, compact programming language. It was developed from 1973 to 1974 by Lee E. McMahon of Bell Labs, and is available today for most operating systems. sed w ...
regular expressions, particularly those using the "s" operator, are much similar to Perl (sed is a predecessor to Perl). The default delimiter is "/", but any delimiter can be used; the default is "s/regexp/replacement/", but "s:regexp:replacement:" is also a valid form. For example, to match a "pub" directory (as in the Perl example) and replace it with "foo", the default (escaping the slashes) is
s/ftp:\/\/ \/\/pub\//foo/
Using an exclamation point ("!") as delimiter instead yields s!ftp:// //pub/!foo!


See also

*
Magic quotes Magic quotes was a feature of the PHP scripting language, wherein strings are automatically escaped—special characters are prefixed with a backslash—before being passed on. It was introduced to help newcomers write functioning SQL comm ...
*
String literal A string literal or anonymous string is a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally " bracketed delimiters", as in x = "foo", where "foo" is a string ...


References

{{reflist Software engineering folklore Perl PHP Articles with example Perl code Regular expressions Articles with example Python (programming language) code