Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8061408

Add support for verbatim string literals

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Other
    • Icon: P4 P4
    • None
    • None
    • specification

      Name: bsC130419 Date: 06/20/2001


      java version "1.3.1"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
      Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)

      The current syntax for string literals, as stated in the JLS, has many
      practical limitations:

      A) String literals that contain many occurances of the '\' character must use
      superfluous escaping. Examples of this limitation are:
      1) Regular expressions
      2) Path syntaxes that use '\' as a path separator, eg. Win32 file paths.

      B) String literals cannot contain line breaks. Often the author of the Java
      source file would like to add structure, by using whitespace, to the String
      literal without breaking the meaning. For instance:

      1) Storing a SQL query as a String literal. Using the current version of the
      Java Language, the source file author can either put the whole SQL statement in
      one line, or break the statement in separate chunks:

      String qry =
      "SELECT a.name, b.number" +
      "FROM table a, table b" +
      "WHERE a.id = b.id" +
      "ORDER BY name";

      The above example is small, but bigger queries quickly start to require a lot
      of tab,",+ typing.


      Feature Request Proposal

      A vertabim string literal would have the following properties:
      1) All characters are treaded vertabim, that is, they are not escaped.
      1a) Character, hexadecimal and Unicode escape sequences are not processed.
      1b) The only character with a special meaning is the tail quote, indicating the
      end of the vertabim string literal.
      2) A vertabim string may span multiple lines, only ending when the end quote
      delimiter is encountered.
      3) A vertabim string literal is indicated by a '@' character immediately before
      the opening quote: String re = @"\(\d{1,3}\D{1,3)";

        To summarize, I propose adding vertabim String literals as formulated in the C#
      Language Specification[1] submitted to the ECMA, paragraph 9.3.4.5.
      A similar enhancement is proposed for the next version of ECMAScript (v4).

      A number of examples follow to summarize the proposed syntax, and show the
      advantage of vertabim string literals.


      Examples
      (string literal, result string in comment)

      String a = "hello, world"; // hello, world
      String b = @"hello, world"; // hello, world

      String c = "hello \t world"; // hello world
      String d = @"hello \t world"; // hello \t world

      String e = "Joe said \"Hello\" to me"; // Joe said "Hello"
      String f = @"Joe said ""Hello"" to me"; // Joe said "Hello"

      String g = "\\\\server\\share\\file.txt"; // \\server\share\file.txt
      String h = @"\\server\share\file.txt"; // \\server\share\file.txt

      // A simple query, much more easily separated over multiple lines.
      // Also note that indenting is made much easier for these type of text.

      String qry = @"
         SELECT
           a.name, b.number
         FROM
           User a, Data b
         WHERE
           a.name = "James"
            AND
           a.id = b.id
      ";

      // A simple regular expression, without the need for erroneous escaping:

      String RE = new RE(@"\D{1,3}\S{1,3};");
      (Review ID: 126782)
      ======================================================================

            abuckley Alex Buckley
            abuckley Alex Buckley
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: