Annotation of embedaddon/php/ext/json/utf8_to_utf16.c, revision 1.1.1.2

1.1       misho       1: /* utf8_to_utf16.c */
                      2: 
                      3: /* 2005-12-25 */
                      4: 
                      5: /*
                      6: Copyright (c) 2005 JSON.org
                      7: 
                      8: Permission is hereby granted, free of charge, to any person obtaining a copy
                      9: of this software and associated documentation files (the "Software"), to deal
                     10: in the Software without restriction, including without limitation the rights
                     11: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
                     12: copies of the Software, and to permit persons to whom the Software is
                     13: furnished to do so, subject to the following conditions:
                     14: 
                     15: The above copyright notice and this permission notice shall be included in all
                     16: copies or substantial portions of the Software.
                     17: 
                     18: The Software shall be used for Good, not Evil.
                     19: 
                     20: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
                     21: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
                     22: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
                     23: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
                     24: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
                     25: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
                     26: SOFTWARE.
                     27: */
                     28: 
                     29: #include "utf8_to_utf16.h"
                     30: #include "utf8_decode.h"
                     31: 
                     32: int 
1.1.1.2 ! misho      33: utf8_to_utf16(unsigned short *w, char p[], int length) 
1.1       misho      34: {
                     35:     int c;
                     36:     int the_index = 0;
                     37:     json_utf8_decode utf8;
                     38:     
                     39:     utf8_decode_init(&utf8, p, length);
                     40:     for (;;) {
                     41:         c = utf8_decode_next(&utf8);
                     42:         if (c < 0) {
                     43:             return (c == UTF8_END) ? the_index : UTF8_ERROR;
                     44:         }
                     45:         if (c < 0x10000) {
1.1.1.2 ! misho      46:             if (w) {
        !            47:                 w[the_index] = (unsigned short)c;
        !            48:             }
1.1       misho      49:             the_index += 1;
                     50:         } else {
                     51:             c -= 0x10000;
1.1.1.2 ! misho      52:             if (w) {
        !            53:                 w[the_index] = (unsigned short)(0xD800 | (c >> 10));
        !            54:                 w[the_index + 1] = (unsigned short)(0xDC00 | (c & 0x3FF));
        !            55:             }
        !            56:             the_index += 2;
1.1       misho      57:         }
                     58:     }
                     59: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>