-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringParser.js
72 lines (66 loc) · 1.71 KB
/
stringParser.js
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
const fs = require('fs')
const data = fs.readFileSync(
'D:\\Geekskool\\JSONParser\\inputString.txt',
'utf8'
)
function stringParser (data) {
const ans = []
const specialCharacter = {
'"': '"',
'\\': '\\',
'/': '/',
b: '\b',
f: '\f',
n: '\n',
r: '\r',
t: '\t'
}
// console.log(specialCharacter)
// console.log(data)
if (!data.startsWith('"')) return null
data = data.slice(1)
while (data[0] !== '"') {
if (data[0] === '\n' || data[0] === '\t') return null
if (data[0] === '\\') {
if (specialCharacter[data[1]]) {
ans.push(specialCharacter[data[1]])
data = data.slice(2)
if (data.includes('"')) continue
return null
}
if (data[1] !== 'u') return null
const hexDigits = data.slice(2, 6)
if (!hexDigits.match(/[a-fA-F0-9]{4}/)) return null
const actualCharacter = String.fromCodePoint(parseInt(hexDigits, 16))
if (!actualCharacter) return null
data = data.slice(6)
ans.push(actualCharacter)
continue
}
ans.push(data[0])
data = data.slice(1)
if (data.length === 0) return null
// console.log(data)
// return [ans.join(''), data]
// for (let i = 0; i < data.length; i++) {
// if (data[i] === '\\') {
// // console.log(data[i + 1])
// const temp = specialCharacter[data[i + 1]]
// if (temp) {
// ans.push(temp)
// i += 1
// } else {
// return null
// }
// } else {
// ans.push(data[i])
// }
// }
}
return [ans.join(''), data.slice(1)]
}
console.log(stringParser(data))
// const t = stringParser(data)
// console.log(t)
// console.log([t, 0])
module.exports = stringParser