-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstrdec.cpp
More file actions
85 lines (75 loc) · 2.27 KB
/
strdec.cpp
File metadata and controls
85 lines (75 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Copyright (c) 2022 Light Labs Inc.
* All Rights Reserved
* Licensed under the MIT license.
*/
#include "strdec.h"
Unicode IDecoder::decode( const char * in ) // TODO remove, replace with parse() alone
{
Unicode out;
parse( out, in );
return out;
}
int UTF8Homebrew::trailCount( char & c )
{
return ( c & 0x80 ) ?
( c & 0x40 ) ?
( c & 0x20 ) ?
( c & 0x10 ) ?
( c & 0x08 ) ? FORBIDDEN
: ( c &= 0x07, 3 ) // extended planes; use 3 bits
: ( c &= 0x0f, 2 ) // basic multilingual; use 4 bits
: ( c &= 0x1f, 1 ) // European alphabets; use 5 bits
: ( c &= 0x3f, CONTINUED ) // use 6 bits
: 0; // ANSI; leave the character as is
}
void UTF8Homebrew::parse( Unicode & out, const char * source )
{
out.clear();
int trailing = 0;
wchar_t wc;
char octet;
while( ( octet = *source++ ) )
{
int leftover = trailCount( octet );
if( leftover == CONTINUED )
{
if( trailing ) { wc |= octet << ( --trailing * 6 ); }
else { wc = UnChar::UCS2; } // unexpected continuation
}
else
{
if( trailing ) { out.push_back( UnChar::UCS2 ); } // unexpected restart
if( leftover == FORBIDDEN ) { wc = UnChar::UCS2; trailing = 0; }
else { wc = octet << ( trailing = leftover ) * 6; }
}
if( !trailing ) { out.push_back( wc ); }
}
if( trailing ) { out.push_back( UnChar::UCS2 ); }
}
void ISO88591Flat::parse( Unicode & out, const char * source )
{
out.clear();
char octet;
while( ( octet = *source++ ) ) { out.push_back( octet ); }
}
#ifdef USE_GNU_STDIO_FILEBUF
RawStreamBuf::RawStreamBuf()
: fd( memfd_open( "conv", O_RDWR ) )
, fb( fd, std::ios_base::in )
, is( &fb ) {}
RawStreamBuf::~RawStreamBuf() { close( fd ); }
void RawStreamBuf::parse( Unicode & out, const char * source )
{
auto len = strlen( source ) + 1;
pwrite( fd, source, len, 0 );
lseek( fd, 0, SEEK_SET );
out.resize( len );
is.clear();
is.getline( &out[0], len );
out.resize( wcslen( &out[0] ) );
//auto size = is.gcount();
//bool fail = !size || ( is.rdstate() & std::ios_base::failbit );
//if( fail ) { perror( "Conversion" ); }
}
#endif