-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValid_UTF8.cpp
More file actions
108 lines (98 loc) · 1.87 KB
/
Valid_UTF8.cpp
File metadata and controls
108 lines (98 loc) · 1.87 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "stdafx.h"
#include "iostream"
#include "vector"
#include "string"
#include "map"
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <stack>
#include <sstream>
#include <iomanip>
#include <random>
#include <assert.h>
#include <iterator>
#include <queue>
#include <math.h>
#include <vector>
#include <set>
using namespace std;
int getWordWidth(char* p)
{
int t=*p;
int count=0;
while(t&128)
{
t=t<<1;
count++;
}
if(count==0)count=1;
return count;
}
bool isValidContinuation(char *p)
{
int t=*p;
return (t&128)!=0&&(t&64)==0;
}
bool isValidUTF8(char *p)
{
if(!p)return false;
int n=strlen(p),i=0;
while(i<n)
{
int count=getWordWidth(p+i);
for(int j=1;j<count;j++)
if(i+j>=n||!isValidContinuation(p+i+j))
return false;
i=i+count;
}
return true;
}
void testIsValidUTF8_NULLPointer()
{
assert(isValidUTF8(NULL)==false);
}
void testIsValidUTF8_OneByte()
{
char* p="\x00";
assert(isValidUTF8(p)==true);
p="\x0A";
assert(isValidUTF8(p)==true);
p="\xC0";
assert(isValidUTF8(p)==false);
}
void testIsValidUTF8_TwoByte()
{
char* p="\x04\x00";
assert(isValidUTF8(p)==true);
p="\xC0\xA0";
assert(isValidUTF8(p)==true);
p="\xC0\xC0";
assert(isValidUTF8(p)==false);
p="\x00\xC0";
assert(isValidUTF8(p)==true);
p="\x01\xC0";
assert(isValidUTF8(p)==false);
}
void testIsValidUTF8_FiveByte()
{
char* p="\xFA\xA0\xA0\xA0\xA0";
assert(isValidUTF8(p)==true);
p="\xFA\xA0\x00\xA0\xA0";
assert(isValidUTF8(p)==false);
}
void testIsValidUTF8_NineByte()
{
char* p="\xFA\xA0\xA0\xA0\xA0\xA0\xA0\xA0\01";
assert(isValidUTF8(p)==true);
p="\xFA\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xC1";
assert(isValidUTF8(p)==false);
}
int _tmain(int argc, _TCHAR* argv[])
{
testIsValidUTF8_OneByte();
testIsValidUTF8_TwoByte();
testIsValidUTF8_FiveByte();
testIsValidUTF8_NineByte();
return 0;
}