E07903: a unicode escape needs exactly four hex digits (0-9, a-f, A-F) - write \\u for a backslash and a 'u'

Category: E07xxx Full resolution · Reported in phase: SYMBOL_DEFINITION · Classification: MALFORMED_UNICODE_ESCAPE · All error codes

What went wrong

A backslash-u in a String is not followed by exactly four hex digits. In a String, a backslash then 'u' starts a unicode escape, which needs four hex digits (0-9, a-f, A-F): "\u00e9" is an e with an acute accent. Anything else - a sign ("\u+041"), too few digits ("\u004"), a non-ASCII digit, or simply text such as the 'u' in a Windows path ("C:\users") - is not an escape. It used to be decoded loosely: "\u+041" became 'A' and "\u-001" became U+FFFF.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 1394 (Why must a constant's String be written with double quotes?)

How to fix it

For a unicode character, give exactly four hex digits:

  • "A\u+041B" --> "A\u0041B"

For a backslash followed by a 'u', escape the backslash:

  • "C:\users\steve" --> "C:\\users\\steve"

Common causes

  • A Windows path written with single backslashes
  • An escape copied with a missing or extra digit
  • A sign or space inside the escape

See also

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E07903: a unicode escape needs exactly four hex digits (0-9, a-f, A-F) - write \\u for a backslash and a 'u'

#!ek9
defines module error.e07903.bad

  defines function

    escapes()
      <- rtn as String?

      signed <- "A\u+041B"

      negative <- "C\u-001D"

      tooShort <- "T\u004"

      inBacktick <- `L\u+041M`

      //An escaped backslash then a 'u' is two plain characters, and a real escape is still fine.
      windowsPath <- "C:\\users\\steve"
      letter <- "\u0041"
      rtn: signed + negative + tooShort + inBacktick + windowsPath + letter

Fixed: Escape the backslash, or give four hex digits

#!ek9
defines module error.e07903.good

  defines function

    //Escape the backslash...
    home()
      <- rtn as String: "C:\\users\\steve"

    //...or give exactly four hex digits
    accented()
      <- rtn as String: "caf\u00e9"

Questions and answers

These Q&A entries show E07903 and its fix: