Type Challenges Judge

C-printf Parser

提出詳細

type ControlsMap = { c: 'char' s: 'string' d: 'dec' o: 'oct' h: 'hex' f: 'float' p: 'pointer' } type ParsePrintFormat<S extends string, Acc extends readonly unknown[]=[]> = S extends `${infer S1}${infer S2}` ? S1 extends "%" // parse percent format ? S2 extends `${infer C}${infer S3}` ? C extends "%" // escaped "%" character ? ParsePrintFormat<S3,Acc> : C extends keyof ControlsMap ? ParsePrintFormat<S3, [...Acc, ControlsMap[C]]> : ParsePrintFormat<S3, Acc> // invalid format : Acc // ends with "%" character : ParsePrintFormat<S2,Acc> : Acc
提出日時2023-09-19 04:15:30
問題C-printf Parser
ユーザーsankantsu
ステータスAccepted
テストケース
import type { Equal, Expect } from '@type-challenges/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 %'>, []>>, ]