1 /*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
530 char* end = buf + buflen;
531 for (int index = 0; index < length; index++) {
532 T c = base[index];
533 if (c >= 32 && c < 127) {
534 if (p + 1 >= end) break; // string is truncated
535 *p++ = (char)c;
536 } else {
537 if (p + 6 >= end) break; // string is truncated
538 sprintf(p, "\\u%04x", c);
539 p += 6;
540 }
541 }
542 *p = '\0';
543 }
544
545 // Explicit instantiation for all supported types.
546 template int UNICODE::quoted_ascii_length<jbyte>(jbyte* base, int length);
547 template int UNICODE::quoted_ascii_length<jchar>(jchar* base, int length);
548 template void UNICODE::as_quoted_ascii<jbyte>(const jbyte* base, int length, char* buf, int buflen);
549 template void UNICODE::as_quoted_ascii<jchar>(const jchar* base, int length, char* buf, int buflen);
550
551
552 #ifndef PRODUCT
553 void TestAsUtf8() {
554 char res[60];
555 jchar str[20];
556
557 for (int i = 0; i < 20; i++) {
558 str[i] = 0x0800; // char that is 2B in UTF-16 but 3B in UTF-8
559 }
560 str[19] = (jchar)'\0';
561
562 // The resulting string in UTF-8 is 3*19 bytes long, but should be truncated
563 UNICODE::as_utf8(str, 19, res, 10);
564 assert(strlen(res) == 9, "string should be truncated here");
565
566 UNICODE::as_utf8(str, 19, res, 18);
567 assert(strlen(res) == 15, "string should be truncated here");
568
569 UNICODE::as_utf8(str, 19, res, 20);
570 assert(strlen(res) == 18, "string should be truncated here");
571
572 // Test with an "unbounded" buffer
573 UNICODE::as_utf8(str, 19, res, INT_MAX);
574 assert(strlen(res) == 3*19, "string should end here");
575 }
576 #endif
|
1 /*
2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
530 char* end = buf + buflen;
531 for (int index = 0; index < length; index++) {
532 T c = base[index];
533 if (c >= 32 && c < 127) {
534 if (p + 1 >= end) break; // string is truncated
535 *p++ = (char)c;
536 } else {
537 if (p + 6 >= end) break; // string is truncated
538 sprintf(p, "\\u%04x", c);
539 p += 6;
540 }
541 }
542 *p = '\0';
543 }
544
545 // Explicit instantiation for all supported types.
546 template int UNICODE::quoted_ascii_length<jbyte>(jbyte* base, int length);
547 template int UNICODE::quoted_ascii_length<jchar>(jchar* base, int length);
548 template void UNICODE::as_quoted_ascii<jbyte>(const jbyte* base, int length, char* buf, int buflen);
549 template void UNICODE::as_quoted_ascii<jchar>(const jchar* base, int length, char* buf, int buflen);
|