?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. /*-------------------------------------------------------------------------
  2.    mbstoc16s.c - convert a multibyte string to a wide character string
  3.  
  4.    Copyright (C) 2018, Philipp Klaus Krause, krauseph@informatik.uni-freiburg.de
  5.  
  6.    This library is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by the
  8.    Free Software Foundation; either version 2, or (at your option) any
  9.    later version.
  10.  
  11.    This library is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this library; see the file COPYING. If not, write to the
  18.    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  19.    MA 02110-1301, USA.
  20.  
  21.    As a special exception, if you link this library with other files,
  22.    some of which are compiled with SDCC, to produce an executable,
  23.    this library does not by itself cause the resulting executable to
  24.    be covered by the GNU General Public License. This exception does
  25.    not however invalidate any other reasons why the executable file
  26.    might be covered by the GNU General Public License.
  27. -------------------------------------------------------------------------*/
  28.  
  29. #include <uchar.h>
  30.  
  31. #include <stdlib.h>
  32. #include <limits.h>
  33. #include <wchar.h>
  34.  
  35. #ifndef __STDC_UTF_16__
  36. #error Encoding for char16_t strings must be UTF-16
  37. #endif
  38. #ifndef __STDC_ISO_10646__
  39. #error Encoding for wchar_t strings must be UCS-4
  40. #endif
  41.  
  42. // This implementation assumes that mbtowc() does not keep internal state
  43. // and that mbtowc() does not read beyond a terminating 0.
  44. size_t __mbstoc16s(char16_t *restrict c16s, const char *restrict s, size_t n)
  45. {
  46.         size_t m = 0;
  47.  
  48.         for(;;)
  49.         {
  50.                 int l;
  51.                 wchar_t codepoint;
  52.  
  53.                 l = mbtowc(&codepoint, s, MB_LEN_MAX);
  54.  
  55.                 if(l < 0)
  56.                         return(-1);
  57.                 else if(!l)
  58.                 {
  59.                         if(m < n)
  60.                                 *c16s = 0;
  61.                         break;
  62.                 }
  63.  
  64.                 if (codepoint <= 0xffff) // Basic multilingual plane
  65.                 {
  66.                         if (m >= n)
  67.                                 break;
  68.  
  69.                         *c16s++ = codepoint;
  70.                         m++;
  71.                 }
  72.                 else
  73.                 {
  74.                         if (m + 1 >= n)
  75.                                 break;
  76.  
  77.                         codepoint -= 0x100000;
  78.                         *c16s++ = ((codepoint >> 10) & 0x3ff) + 0xd800;
  79.                         *c16s++ = (codepoint & 0x3ff) + 0xdc00;
  80.                         m += 2;
  81.                 }
  82.                 s += l;
  83.         }
  84.        
  85.         return(m);
  86. }
  87.  
  88.