]> granicus.if.org Git - postgresql/blob - src/backend/utils/mb/Unicode/ucs2utf.pl
Update copyright for 2009.
[postgresql] / src / backend / utils / mb / Unicode / ucs2utf.pl
1 #
2 # Copyright (c) 2001-2009, PostgreSQL Global Development Group
3 #
4 # $PostgreSQL: pgsql/src/backend/utils/mb/Unicode/ucs2utf.pl,v 1.5 2009/01/01 17:23:51 momjian Exp $
5 # convert UCS-4 to UTF-8
6 #
7 sub ucs2utf {
8         local($ucs) = @_;
9         local $utf;
10
11         if ($ucs <= 0x007f) {
12                 $utf = $ucs;
13         } elsif ($ucs > 0x007f && $ucs <= 0x07ff) {
14                 $utf = (($ucs & 0x003f) | 0x80) | ((($ucs >> 6) | 0xc0) << 8);
15         } elsif ($ucs > 0x07ff && $ucs <= 0xffff) {
16                 $utf = ((($ucs >> 12) | 0xe0) << 16) | 
17                         (((($ucs & 0x0fc0) >> 6) | 0x80) << 8) |
18                                 (($ucs & 0x003f) | 0x80);
19         } else {
20            $utf = ((($ucs >> 18) | 0xf0) << 24) |
21                    (((($ucs & 0x3ffff) >> 12) | 0x80) << 16) |            
22                    (((($ucs & 0x0fc0) >> 6) | 0x80) << 8) |
23                    (($ucs & 0x003f) | 0x80);
24     }
25         return($utf);
26 }
27 1;