1

I have a problem, I've got an escaped string for example "\\u0026" and I need this to transform to unicode char '\u0026'.

Tricks like string_concat('\\', S, "\\u0026"), write(S). didn't help, because it will remove \ not only the escape . So basically my problem is, how to remove escape chars from the string.

EDIT: Oh, I've just noticed, that stackoverflow also plays with escape \.

write_canonical/1 gives me "\\u0026", how to transform that into a single '&' char?

  • I really have trouble understanding your question. Can you try to formulate it differently? What is an "escaped string"? Did you try using `string_chars/2`? Are you talking about a string the data type or some kind of a string literal? (A hint: you can try to use backticks for `inline code`. –  Oct 07 '16 at 07:13
  • You can just paste the output of `write_canonical/1` for the string you have initially. Is it `"&"` or `"\\u0026"`? –  Oct 07 '16 at 09:43
  • If you use the notation `'\x17f\'` instead! Thus `?- '\x17f\' = ſ.` holds! – false Oct 07 '16 at 15:37
  • Escaped string is badly formulated, I mean escaped char, like you cannot write '\' in, but you have to add escape character before, so '\\'. The thing is, write_canocial will write "\\u0026" and I don't know how to transform that into "&". – Floctioncers Oct 07 '16 at 20:03

2 Answers2

2

In ISO Prolog a char is usually considered an atom of length 1. Atoms and chars are enclosed in single quotes, or written without quotes if possible. Here are some examples:

?- X = abc.       /* an atom, but not a char */
X = abc
?- X = a.         /* an atom and also a char */
X = a
?- X = '\u0061'. 
X = a

The \u notation is SWI-Prolog specific, and not found in the ISO Prolog. In SWI-Prolog there is a data type string again not found in the ISO Prolog, and always enclosed in double quotes. Here are some examples:

?- X = "abc".    /* a string */
X = "abc"
?- X = "a".      /* again a string */
X = "a"
?- X = "\u0061".
X = "a"

If you have a string at hand of length 1, you can convert it to a char via the predicate atom_string/2. This is a SWI-Prolog specific predicate, not in ISO Prolog:

?- atom_string(X, "\u0061").
X = a
?- atom_string(X, "\u0026").
X = &

Some recommendation. Start learning the ISO Prolog atom predicates first, there are quite a number. Then learn the SWI-Prolog atom and string predicates.

You dont have to learn so many new SWI-Prolog predicates, since in SWI-Prolog most of the ISO Prolog predicates also accept strings. Here is an example of the ISO Prolog predicate atom_codes/2 used with a string in the first argument:

?- atom_codes("\u0061\u0026", L).
L = [97, 38].
?- L = [0'\u0061, 0'\u0026].
L = [97, 38].
?- L = [0x61, 0x26].
L = [97, 38].

P.S: The 0' notation is defined in the ISO Prolog, its neither a char, atom or string, but it represents an integer data type. The value is the code of the given char after the 0'. I have combined it with the SWI-Prolog \u notation.

P.P.S: The 0' notation in connection of the \u notation is of course redundant, in ISO Prolog one can directly use the hex notation prefix 0x for integer values.

Community
  • 1
  • 1
  • Thank you, nice explanation. But I'm still not sure, how to transform this "\\u0026" - it's string in SWI-Prolog, the thing that write_canonical/1 gives me - to '&', one single char. It's not quite a unicode code, because the \ is escaped wit another \. I hope I've explained it better this time. – Floctioncers Oct 07 '16 at 20:09
  • & is a unicode character, it even an ascii character: http://www.fileformat.info/info/unicode/char/0026/index.htm In ISO terminology there are chars and codes, if you want to write the code use atom_codes/2 before write_canonical/1. Or directly 0x notation, you can also enter hex numbers in ISO Prolog. –  Oct 07 '16 at 20:13
  • If you have an atom of length 4 such as '0026' and would like to convert it from hex into decimal, you might take a look at DCG, definite clause grammars. Or directly code a predicate, such as here http://osdir.com/ml/ai.prolog.swi/2003-06/msg00013.html After you have converted it to decimal you can use the predicate char_code/2. –  Oct 07 '16 at 20:20
  • Oh, right! Great idea, I it would be a nice excercise in Prolog to make a nice predicate to convert hex into dec and other way around. (Main problem was, I didn't know the char codes are unicodes in decimal, thanks!) – Floctioncers Oct 07 '16 at 20:32
  • It depends on the Prolog system, in some Prolog systems one has even the choice between different codings. In SWI-Prolog they use unicode. ISO Prolog only requires ASCII. Everything else is an implementation specific extension. –  Oct 07 '16 at 21:07
0

The thing is that "\\u0026" is already what you are searching for because it represents \u0026.

marli
  • 529
  • 10
  • 19