-
Notifications
You must be signed in to change notification settings - Fork 0
/
00147-hard-c-printf-parser.ts
33 lines (29 loc) · 1.09 KB
/
00147-hard-c-printf-parser.ts
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
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
type cases = [
Expect<Equal<ParsePrintFormat<''>, []>>,
Expect<Equal<ParsePrintFormat<'Any string.'>, []>>,
Expect<Equal<ParsePrintFormat<'The result is %d.'>, ['dec']>>,
Expect<Equal<ParsePrintFormat<'The result is %%d.'>, []>>,
Expect<Equal<ParsePrintFormat<'The result is %%%d.'>, ['dec']>>,
Expect<Equal<ParsePrintFormat<'The result is %f.'>, ['float']>>,
Expect<Equal<ParsePrintFormat<'The result is %h.'>, ['hex']>>,
Expect<Equal<ParsePrintFormat<'The result is %q.'>, []>>,
Expect<Equal<ParsePrintFormat<'Hello %s: score is %d.'>, ['string', 'dec']>>,
Expect<Equal<ParsePrintFormat<'The result is %'>, []>>,
]
// ============= Your Code Here =============
type ControlsMap = {
c: 'char'
s: 'string'
d: 'dec'
o: 'oct'
h: 'hex'
f: 'float'
p: 'pointer'
}
type ParsePrintFormat<S extends string, Res extends any[] = []> = S extends `${string}%${infer C}${infer R}`
? C extends keyof ControlsMap
? ParsePrintFormat<R, [...Res, ControlsMap[C]]>
: ParsePrintFormat<R, Res>
: Res