| 360 | } |
| 361 | |
| 362 | string unicode_escape(int in, bool char_literal = false) |
| 363 | { |
| 364 | switch (in) { |
| 365 | case '\'': |
| 366 | case '\"': |
| 367 | case '\\': |
| 368 | return string("\\") + static_cast<char>(in); |
| 369 | case '\b': |
| 370 | return "\\b"; |
| 371 | case '\f': |
| 372 | return "\\f"; |
| 373 | case '\n': |
| 374 | return "\\n"; |
| 375 | case '\r': |
| 376 | return "\\r"; |
| 377 | case '\t': |
| 378 | return "\\t"; |
| 379 | } |
| 380 | |
| 381 | if (in <= UCHAR_MAX && isprint(in)) { |
| 382 | return string(1, static_cast<char>(in)); |
| 383 | } |
| 384 | |
| 385 | if (in > 0x10ffff) { |
| 386 | return "error: out-of-range character"; |
| 387 | } |
| 388 | |
| 389 | if (in <= 0xffff) { |
| 390 | string escaped = "\\u____"; |
| 391 | ACE_OS::snprintf(&escaped[2], 5, "%04x", in); |
| 392 | return escaped; |
| 393 | } |
| 394 | |
| 395 | if (char_literal) { |
| 396 | return "error: char literals larger than \\uffff are not valid in Java"; |
| 397 | } |
| 398 | |
| 399 | // Java uses a UTF-16 Surrogate Pair to represent larger characters, with |
| 400 | // each surrogate represented as its own \uXXXX escape in the source code. |
| 401 | const unsigned int lead = 0xd7c0 + static_cast<unsigned>(in >> 10); |
| 402 | const unsigned int trail = 0xdc0 + static_cast<unsigned>(in & 0x3ff); |
| 403 | string escaped(13, '\0'); |
| 404 | ACE_OS::snprintf(&escaped[0], escaped.size(), "\\u%04x\\u%04x", lead, trail); |
| 405 | return escaped; |
| 406 | } |
| 407 | |
| 408 | string unicode_escape(const char* in) |
| 409 | { |
no test coverage detected