-
Notifications
You must be signed in to change notification settings - Fork 810
/
Copy pathprintf.fs
1480 lines (1279 loc) · 69.6 KB
/
printf.fs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Microsoft.FSharp.Core
open System
open System.IO
open System.Text
open System.Collections.Concurrent
open System.Diagnostics
open System.Globalization
open System.Reflection
open Microsoft.FSharp.Core
open Microsoft.FSharp.Core.Operators
open Microsoft.FSharp.Collections
open LanguagePrimitives.IntrinsicOperators
type PrintfFormat<'Printer, 'State, 'Residue, 'Result>
[<DebuggerStepThrough>]
(value:string, captures: objnull array, captureTys: Type array) =
[<DebuggerStepThrough>]
new (value) = new PrintfFormat<'Printer, 'State, 'Residue, 'Result>(value, null, null)
member _.Value = value
member _.Captures = captures
member _.CaptureTypes = captureTys
override _.ToString() = value
type PrintfFormat<'Printer, 'State, 'Residue, 'Result, 'Tuple>
[<DebuggerStepThrough>]
(value:string, captures, captureTys: Type array) =
inherit PrintfFormat<'Printer, 'State, 'Residue, 'Result>(value, captures, captureTys)
[<DebuggerStepThrough>]
new (value) = new PrintfFormat<'Printer, 'State, 'Residue, 'Result, 'Tuple>(value, null, null)
type Format<'Printer, 'State, 'Residue, 'Result> = PrintfFormat<'Printer, 'State, 'Residue, 'Result>
type Format<'Printer, 'State, 'Residue, 'Result, 'Tuple> = PrintfFormat<'Printer, 'State, 'Residue, 'Result, 'Tuple>
[<AutoOpen>]
module internal PrintfImpl =
/// Basic idea of implementation:
/// Every Printf.* family should returns curried function that collects arguments and then somehow prints them.
/// Idea - instead of building functions on fly argument by argument we instead introduce some predefined parts and then construct functions from these parts
/// Parts include:
/// Plain ones:
/// 1. Final pieces (1..5) - set of functions with arguments number 1..5.
/// Primary characteristic - these functions produce final result of the *printf* operation
/// 2. Chained pieces (1..5) - set of functions with arguments number 1..5.
/// Primary characteristic - these functions doesn not produce final result by itself, instead they tailed with some another piece (chained or final).
/// Plain parts correspond to simple format specifiers (that are projected to just one parameter of the function, say %d or %s). However we also have
/// format specifiers that can be projected to more than one argument (i.e %a, %t or any simple format specified with * width or precision).
/// For them we add special cases (both chained and final to denote that they can either return value themselves or continue with some other piece)
/// These primitives allow us to construct curried functions with arbitrary signatures.
/// For example:
/// - function that corresponds to %s%s%s%s%s (string -> string -> string -> string -> string -> T) will be represented by one piece final 5.
/// - function that has more that 5 arguments will include chained parts: %s%s%s%s%s%d%s => chained2 -> final 5
/// Primary benefits:
/// 1. creating specialized version of any part requires only one reflection call. This means that we can handle up to 5 simple format specifiers
/// with just one reflection call
/// 2. we can make combinable parts independent from particular printf implementation. Thus final result can be cached and shared.
/// i.e when first call to printf "%s %s" will trigger creation of the specialization. Subsequent calls will pick existing specialization
[<Flags>]
type FormatFlags =
| None = 0
| LeftJustify = 1
| PadWithZeros = 2
| PlusForPositives = 4
| SpaceForPositives = 8
let inline hasFlag flags (expected: FormatFlags) = (flags &&& expected) = expected
let inline isLeftJustify flags = hasFlag flags FormatFlags.LeftJustify
let inline isPadWithZeros flags = hasFlag flags FormatFlags.PadWithZeros
let inline isPlusForPositives flags = hasFlag flags FormatFlags.PlusForPositives
let inline isSpaceForPositives flags = hasFlag flags FormatFlags.SpaceForPositives
/// Used for width and precision to denote that user has specified '*' flag
[<Literal>]
let StarValue = -1
/// Used for width and precision to denote that corresponding value was omitted in format string
[<Literal>]
let NotSpecifiedValue = -2
[<System.Diagnostics.DebuggerDisplayAttribute("{ToString()}")>]
[<NoComparison; NoEquality>]
type FormatSpecifier =
{
TypeChar: char
Precision: int
Width: int
Flags: FormatFlags
InteropHoleDotNetFormat: string voption
}
member spec.IsStarPrecision = (spec.Precision = StarValue)
member spec.IsPrecisionSpecified = (spec.Precision <> NotSpecifiedValue)
member spec.IsStarWidth = (spec.Width = StarValue)
member spec.IsWidthSpecified = (spec.Width <> NotSpecifiedValue)
member spec.ArgCount =
let n =
if spec.TypeChar = 'a' then 2
elif spec.IsStarWidth || spec.IsStarPrecision then
if spec.IsStarWidth = spec.IsStarPrecision then 3
else 2
else 1
let n = if spec.TypeChar = '%' then n - 1 else n
assert (n <> 0)
n
override spec.ToString() =
let valueOf n = match n with StarValue -> "*" | NotSpecifiedValue -> "-" | n -> n.ToString()
System.String.Format
(
"'{0}', Precision={1}, Width={2}, Flags={3}",
spec.TypeChar,
(valueOf spec.Precision),
(valueOf spec.Width),
spec.Flags
)
member spec.IsDecimalFormat =
spec.TypeChar = 'M'
member spec.GetPadAndPrefix allowZeroPadding =
let padChar = if allowZeroPadding && isPadWithZeros spec.Flags then '0' else ' ';
let prefix =
if isPlusForPositives spec.Flags then "+"
elif isSpaceForPositives spec.Flags then " "
else ""
padChar, prefix
member spec.IsGFormat =
spec.IsDecimalFormat || System.Char.ToLower(spec.TypeChar) = 'g'
/// Set of helpers to parse format string
module private FormatString =
let intFromString (s: string) (i: byref<int>) =
let mutable res = 0
while (Char.IsDigit s.[i]) do
let n = int s.[i] - int '0'
res <- res * 10 + n
i <- i + 1
res
let parseFlags (s: string) (i: byref<int>) =
let mutable flags = FormatFlags.None
let mutable fin = false
while not fin do
match s.[i] with
| '0' ->
flags <- flags ||| FormatFlags.PadWithZeros
i <- i + 1
| '+' ->
flags <- flags ||| FormatFlags.PlusForPositives
i <- i + 1
| ' ' ->
flags <- flags ||| FormatFlags.SpaceForPositives
i <- i + 1
| '-' ->
flags <- flags ||| FormatFlags.LeftJustify
i <- i + 1
| _ ->
fin <- true
flags
let parseWidth (s: string) (i: byref<int>) =
if s.[i] = '*' then
i <- i + 1
StarValue
elif Char.IsDigit s.[i] then
intFromString s (&i)
else
NotSpecifiedValue
let parsePrecision (s: string) (i: byref<int>) =
if s.[i] = '.' then
if s.[i + 1] = '*' then
i <- i + 2
StarValue
elif Char.IsDigit s.[i + 1] then
i <- i + 1
intFromString s (&i)
else raise (ArgumentException("invalid precision value"))
else
NotSpecifiedValue
let parseTypeChar (s: string) (i: byref<int>) =
let res = s.[i]
i <- i + 1
res
let parseInterpolatedHoleDotNetFormat typeChar (s: string) (i: byref<int>) =
if typeChar = 'P' then
if i < s.Length && s.[i] = '(' then
let i2 = s.IndexOf(")", i)
if i2 = -1 then
ValueNone
else
let res = s.[i+1..i2-1]
i <- i2+1
ValueSome res
else
ValueNone
else
ValueNone
// Skip %P() added for hole in "...%d{x}..."
let skipInterpolationHole typeChar (fmt: string) (i: byref<int>) =
if typeChar <> 'P' then
if i+1 < fmt.Length && fmt.[i] = '%' && fmt.[i+1] = 'P' then
i <- i + 2
if i+1 < fmt.Length && fmt.[i] = '(' && fmt.[i+1] = ')' then
i <- i+2
let findNextFormatSpecifier (s: string) (i: byref<int>) =
let buf = StringBuilder()
let mutable fin = false
while not fin do
if i >= s.Length then
fin <- true
else
let c = s.[i]
if c = '%' then
if i + 1 < s.Length then
let mutable i2 = i+1
let _ = parseFlags s &i2
let w = parseWidth s &i2
let p = parsePrecision s &i2
let typeChar = parseTypeChar s &i2
// shortcut for the simplest case
// if typeChar is not % or it has star as width\precision - resort to long path
if typeChar = '%' && not (w = StarValue || p = StarValue) then
buf.Append('%') |> ignore
i <- i2
else
fin <- true
else
raise (ArgumentException("Missing format specifier"))
else
buf.Append c |> ignore
i <- i + 1
buf.ToString()
/// Represents one step in the execution of a format string
[<NoComparison; NoEquality>]
type Step =
| StepWithArg of prefix: string * conv: (objnull -> string)
| StepWithTypedArg of prefix: string * conv: (objnull -> Type -> string)
| StepString of prefix: string
| StepLittleT of prefix: string
| StepLittleA of prefix: string
| StepStar1 of prefix: string * conv: (objnull -> int -> string)
| StepPercentStar1 of prefix: string
| StepStar2 of prefix: string * conv: (objnull -> int -> int -> string)
| StepPercentStar2 of prefix: string
// Count the number of string fragments in a sequence of steps
static member BlockCount(steps: Step array) =
let mutable count = 0
for step in steps do
match step with
| StepWithArg (prefix, _conv) ->
if not (String.IsNullOrEmpty prefix) then count <- count + 1
count <- count + 1
| StepWithTypedArg (prefix, _conv) ->
if not (String.IsNullOrEmpty prefix) then count <- count + 1
count <- count + 1
| StepString prefix ->
if not (String.IsNullOrEmpty prefix) then count <- count + 1
| StepLittleT(prefix) ->
if not (String.IsNullOrEmpty prefix) then count <- count + 1
count <- count + 1
| StepLittleA(prefix) ->
if not (String.IsNullOrEmpty prefix) then count <- count + 1
count <- count + 1
| StepStar1(prefix, _conv) ->
if not (String.IsNullOrEmpty prefix) then count <- count + 1
count <- count + 1
| StepPercentStar1(prefix) ->
if not (String.IsNullOrEmpty prefix) then count <- count + 1
count <- count + 1
| StepStar2(prefix, _conv) ->
if not (String.IsNullOrEmpty prefix) then count <- count + 1
count <- count + 1
| StepPercentStar2(prefix) ->
if not (String.IsNullOrEmpty prefix) then count <- count + 1
count <- count + 1
count
/// Abstracts generated printer from the details of particular environment: how to write text, how to produce results etc...
[<AbstractClass>]
type PrintfEnv<'State, 'Residue, 'Result>(state: 'State) =
member _.State = state
abstract Finish: unit -> 'Result
abstract Write: string -> unit
/// Write the result of a '%t' format. If this is a string it is written. If it is a 'unit' value
/// the side effect has already happened
abstract WriteT: 'Residue -> unit
member env.WriteSkipEmpty(s: string) =
if not (String.IsNullOrEmpty s) then
env.Write s
member env.RunSteps (args: objnull array, argTys: Type array, steps: Step array) =
let mutable argIndex = 0
let mutable tyIndex = 0
for step in steps do
match step with
| StepWithArg (prefix, conv) ->
env.WriteSkipEmpty prefix
let arg = args.[argIndex]
argIndex <- argIndex + 1
env.Write(conv arg)
| StepWithTypedArg (prefix, conv) ->
env.WriteSkipEmpty prefix
let arg = args.[argIndex]
let argTy = argTys.[tyIndex]
argIndex <- argIndex + 1
tyIndex <- tyIndex + 1
env.Write(conv arg argTy)
| StepString prefix ->
env.WriteSkipEmpty prefix
| StepLittleT(prefix) ->
env.WriteSkipEmpty prefix
let farg = args.[argIndex]
argIndex <- argIndex + 1
let f = farg :?> ('State -> 'Residue)
env.WriteT(f env.State)
| StepLittleA(prefix) ->
env.WriteSkipEmpty prefix
let farg = args.[argIndex]
argIndex <- argIndex + 1
let arg = args.[argIndex]
argIndex <- argIndex + 1
let f = farg :?> ('State -> objnull -> 'Residue)
env.WriteT(f env.State arg)
| StepStar1(prefix, conv) ->
env.WriteSkipEmpty prefix
let star1 = args.[argIndex] :?> int
argIndex <- argIndex + 1
let arg1 = args.[argIndex]
argIndex <- argIndex + 1
env.Write (conv arg1 star1)
| StepPercentStar1(prefix) ->
argIndex <- argIndex + 1
env.WriteSkipEmpty prefix
env.Write("%")
| StepStar2(prefix, conv) ->
env.WriteSkipEmpty prefix
let star1 = args.[argIndex] :?> int
argIndex <- argIndex + 1
let star2 = args.[argIndex] :?> int
argIndex <- argIndex + 1
let arg1 = args.[argIndex]
argIndex <- argIndex + 1
env.Write (conv arg1 star1 star2)
| StepPercentStar2(prefix) ->
env.WriteSkipEmpty prefix
argIndex <- argIndex + 2
env.Write("%")
env.Finish()
/// Type of results produced by specialization.
///
/// This is a function that accepts a thunk to create PrintfEnv on demand (at the very last
/// application of an argument) and returns a concrete instance of an appropriate curried printer.
///
/// After all arguments are collected, specialization obtains concrete PrintfEnv from the thunk
/// and uses it to output collected data.
///
/// Note the arguments must be captured in an *immutable* collection. For example consider
/// let f1 = printf "%d%d%d" 3 // activation captures '3' (args --> [3])
/// let f2 = f1 4 // same activation captures 4 (args --> [3;4])
/// let f3 = f1 5 // same activation captures 5 (args --> [3;5])
/// f2 7 // same activation captures 7 (args --> [3;4;7])
/// f3 8 // same activation captures 8 (args --> [3;5;8])
///
/// If we captured into an mutable array then these would interfere
type PrintfInitial<'State, 'Residue, 'Result> = (unit -> PrintfEnv<'State, 'Residue, 'Result>)
type PrintfFuncFactory<'Printer, 'State, 'Residue, 'Result> =
delegate of objnull list * PrintfInitial<'State, 'Residue, 'Result> -> 'Printer
[<Literal>]
let MaxArgumentsInSpecialization = 3
let revToArray extra (args: 'T list) =
// We've reached the end, now fill in the array, reversing steps, avoiding reallocating
let n = args.Length
let res = Array.zeroCreate (n+extra)
let mutable j = 0
for arg in args do
res.[n-j-1] <- arg
j <- j + 1
res
type Specializations<'State, 'Residue, 'Result>() =
static member Final0(allSteps) =
PrintfFuncFactory<_, 'State, 'Residue, 'Result>(fun args initial ->
let env = initial()
env.RunSteps(revToArray 0 args, null, allSteps)
)
static member CaptureFinal1<'A>(allSteps) =
PrintfFuncFactory<_, 'State, 'Residue, 'Result>(fun args initial ->
(fun (arg1: 'A) ->
let env = initial()
let argArray = revToArray 1 args
argArray.[argArray.Length-1] <- box arg1
env.RunSteps(argArray, null, allSteps)
)
)
static member CaptureFinal2<'A, 'B>(allSteps) =
PrintfFuncFactory<_, 'State, 'Residue, 'Result>(fun args initial ->
(fun (arg1: 'A) (arg2: 'B) ->
let env = initial()
let argArray = revToArray 2 args
argArray.[argArray.Length-1] <- box arg2
argArray.[argArray.Length-2] <- box arg1
env.RunSteps(argArray, null, allSteps)
)
)
static member CaptureFinal3<'A, 'B, 'C>(allSteps) =
PrintfFuncFactory<_, 'State, 'Residue, 'Result>(fun args initial ->
(fun (arg1: 'A) (arg2: 'B) (arg3: 'C) ->
let env = initial()
let argArray = revToArray 3 args
argArray.[argArray.Length-1] <- box arg3
argArray.[argArray.Length-2] <- box arg2
argArray.[argArray.Length-3] <- box arg1
env.RunSteps(argArray, null, allSteps)
)
)
static member Capture1<'A, 'Tail>(next: PrintfFuncFactory<_, 'State, 'Residue, 'Result>) =
PrintfFuncFactory<_, 'State, 'Residue, 'Result>(fun args initial ->
(fun (arg1: 'A) ->
let args = (box arg1 :: args)
next.Invoke(args, initial) : 'Tail
)
)
static member CaptureLittleA<'A, 'Tail>(next: PrintfFuncFactory<_, 'State, 'Residue, 'Result>) =
PrintfFuncFactory<_, 'State, 'Residue, 'Result>(fun args initial ->
(fun (f: 'State -> 'A -> 'Residue) (arg1: 'A) ->
let args = box arg1 :: box (fun s (arg:objnull) -> f s (unbox arg)) :: args
next.Invoke(args, initial) : 'Tail
)
)
static member Capture2<'A, 'B, 'Tail>(next: PrintfFuncFactory<_, 'State, 'Residue, 'Result>) =
PrintfFuncFactory<_, 'State, 'Residue, 'Result>(fun args initial ->
(fun (arg1: 'A) (arg2: 'B) ->
let args = box arg2 :: box arg1 :: args
next.Invoke(args, initial) : 'Tail
)
)
static member Capture3<'A, 'B, 'C, 'Tail>(next: PrintfFuncFactory<_, 'State, 'Residue, 'Result>) =
PrintfFuncFactory<_, 'State, 'Residue, 'Result>(fun args initial ->
(fun (arg1: 'A) (arg2: 'B) (arg3: 'C) ->
let args = box arg3 :: box arg2 :: box arg1 :: args
next.Invoke(args, initial) : 'Tail
)
)
// Special case for format strings containing just one '%d' etc, i.e. StepWithArg then StepString.
// This avoids allocating an argument array, and unfolds the single iteration of RunSteps.
static member OneStepWithArg<'A>(prefix1, conv1, prefix2) =
PrintfFuncFactory<_, 'State, 'Residue, 'Result>(fun _args initial ->
// Note this is the actual computed/stored closure for
// sprintf "prefix1 %d prefix2"
// for any simple format specifiers, where conv1 and conv2 will depend on the format specifiers etc.
(fun (arg1: 'A) ->
let env = initial()
env.WriteSkipEmpty prefix1
env.Write(conv1 (box arg1))
env.WriteSkipEmpty prefix2
env.Finish())
)
// Special case for format strings containing two simple formats like '%d %s' etc, i.e.
///StepWithArg then StepWithArg then StepString. This avoids allocating an argument array,
// and unfolds the two iteration of RunSteps.
static member TwoStepWithArg<'A, 'B>(prefix1, conv1, prefix2, conv2, prefix3) =
PrintfFuncFactory<_, 'State, 'Residue, 'Result>(fun _args initial ->
// Note this is the actual computed/stored closure for
// sprintf "prefix1 %d prefix2 %s prefix3"
// for any simple format specifiers, where conv1 and conv2 will depend on the format specifiers etc.
(fun (arg1: 'A) (arg2: 'B) ->
let env = initial()
env.WriteSkipEmpty prefix1
env.Write(conv1 (box arg1))
env.WriteSkipEmpty prefix2
env.Write(conv2 (box arg2))
env.WriteSkipEmpty prefix3
env.Finish())
)
let inline (===) a b = Object.ReferenceEquals(a, b)
let inline boolToString v = if v then "true" else "false"
let inline stringToSafeString v =
match v with
| null -> ""
| _ -> v
[<Literal>]
let DefaultPrecision = 6
/// A wrapper struct used to slightly strengthen the types of "ValueConverter" objects produced during composition of
/// the dynamic implementation. These are always functions but sometimes they take one argument, sometimes two.
[<Struct; NoEquality; NoComparison>]
type ValueConverter internal (f: objnull) =
member x.FuncObj = f
static member inline Make (f: objnull -> string) = ValueConverter(box f)
static member inline Make (f: objnull -> int -> string) = ValueConverter(box f)
static member inline Make (f: objnull -> int-> int -> string) = ValueConverter(box f)
let getFormatForFloat (ch: char) (prec: int) = ch.ToString() + prec.ToString()
let normalizePrecision prec = min (max prec 0) 99
/// Contains helpers to convert printer functions to functions that prints value with respect to specified justification
/// There are two kinds to printers:
/// 'T -> string - converts value to string - used for strings, basic integers etc..
/// string -> 'T -> string - converts value to string with given format string - used by numbers with floating point, typically precision is set via format string
/// To support both categories there are two entry points:
/// - withPadding - adapts first category
/// - withPaddingFormatted - adapts second category
module Padding =
/// pad here is function that converts T to string with respect of justification
/// basic - function that converts T to string without applying justification rules
/// adaptPaddedFormatted returns boxed function that has various number of arguments depending on if width\precision flags has '*' value
let adaptPaddedFormatted (spec: FormatSpecifier) getFormat (basic: string -> objnull -> string) (pad: string -> int -> objnull -> string) : ValueConverter =
if spec.IsStarWidth then
if spec.IsStarPrecision then
// width=*, prec=*
ValueConverter.Make (fun v width prec ->
let fmt = getFormat (normalizePrecision prec)
pad fmt width v)
else
// width=*, prec=?
let prec = if spec.IsPrecisionSpecified then normalizePrecision spec.Precision else DefaultPrecision
let fmt = getFormat prec
ValueConverter.Make (fun v width ->
pad fmt width v)
elif spec.IsStarPrecision then
if spec.IsWidthSpecified then
// width=val, prec=*
ValueConverter.Make (fun v prec ->
let fmt = getFormat prec
pad fmt spec.Width v)
else
// width=X, prec=*
ValueConverter.Make (fun v prec ->
let fmt = getFormat prec
basic fmt v)
else
let prec = if spec.IsPrecisionSpecified then normalizePrecision spec.Precision else DefaultPrecision
let fmt = getFormat prec
if spec.IsWidthSpecified then
// width=val, prec=*
ValueConverter.Make (
pad fmt spec.Width)
else
// width=X, prec=*
ValueConverter.Make (
basic fmt)
/// pad here is function that converts T to string with respect of justification
/// basic - function that converts T to string without applying justification rules
/// adaptPadded returns boxed function that has various number of arguments depending on if width flags has '*' value
let adaptPadded (spec: FormatSpecifier) (basic: objnull -> string) (pad: int -> objnull -> string) : ValueConverter =
if spec.IsStarWidth then
// width=*, prec=?
ValueConverter.Make (fun v width ->
pad width v)
else
if spec.IsWidthSpecified then
// width=val, prec=*
ValueConverter.Make (
pad spec.Width)
else
// width=X, prec=*
ValueConverter.Make (
basic)
let withPaddingFormatted (spec: FormatSpecifier) getFormat (defaultFormat: string) (f: string -> objnull -> string) left right : ValueConverter =
if not (spec.IsWidthSpecified || spec.IsPrecisionSpecified) then
ValueConverter.Make (f defaultFormat)
else
if isLeftJustify spec.Flags then
adaptPaddedFormatted spec getFormat f left
else
adaptPaddedFormatted spec getFormat f right
let withPadding (spec: FormatSpecifier) (f: objnull -> string) left right : ValueConverter =
if not spec.IsWidthSpecified then
ValueConverter.Make f
else
if isLeftJustify spec.Flags then
adaptPadded spec f left
else
adaptPadded spec f right
/// Contains functions to handle left/right justifications for non-numeric types (strings/bools)
module Basic =
let leftJustify (f: objnull -> string) padChar =
fun (w: int) v ->
(f v).PadRight(w, padChar)
let rightJustify (f: objnull -> string) padChar =
fun (w: int) v ->
(f v).PadLeft(w, padChar)
let withPadding (spec: FormatSpecifier) f =
let padChar, _ = spec.GetPadAndPrefix false
Padding.withPadding spec f (leftJustify f padChar) (rightJustify f padChar)
/// Contains functions to handle left/right and no justification case for numbers
module GenericNumber =
let inline singleIsPositive n = n >= 0.0f
let inline doubleIsPositive n = n >= 0.0
let inline decimalIsPositive n = n >= 0.0M
let isPositive (n: obj) =
match n with
| :? int8 as n -> n >= 0y
| :? uint8 -> true
| :? int16 as n -> n >= 0s
| :? uint16 -> true
| :? int32 as n -> n >= 0
| :? uint32 -> true
| :? int64 as n -> n >= 0L
| :? uint64 -> true
| :? nativeint as n -> n >= 0n
| :? unativeint -> true
| :? single as n -> singleIsPositive n
| :? double as n -> doubleIsPositive n
| :? decimal as n -> decimalIsPositive n
| _ -> failwith "isPositive: unreachable"
/// handles right justification when pad char = '0'
/// this case can be tricky:
/// - negative numbers, -7 should be printed as '-007', not '00-7'
/// - positive numbers when prefix for positives is set: 7 should be '+007', not '00+7'
let rightJustifyWithZeroAsPadChar (str: string) isNumber isPositive w (prefixForPositives: string) =
System.Diagnostics.Debug.Assert(prefixForPositives.Length = 0 || prefixForPositives.Length = 1)
if isNumber then
if isPositive then
prefixForPositives + (if w = 0 then str else str.PadLeft(w - prefixForPositives.Length, '0')) // save space to
else
if str.[0] = '-' then
let str = str.Substring 1
"-" + (if w = 0 then str else str.PadLeft(w - 1, '0'))
else
str.PadLeft(w, '0')
else
str.PadLeft(w, ' ')
/// handler right justification when pad char = ' '
let rightJustifyWithSpaceAsPadChar (str: string) isNumber isPositive w (prefixForPositives: string) =
System.Diagnostics.Debug.Assert(prefixForPositives.Length = 0 || prefixForPositives.Length = 1)
(if isNumber && isPositive then prefixForPositives + str else str).PadLeft(w, ' ')
/// handles left justification with formatting with 'G'\'g' - either for decimals or with 'g'\'G' is explicitly set
let leftJustifyWithGFormat (str: string) isNumber isInteger isPositive w (prefixForPositives: string) padChar =
if isNumber then
let str = if isPositive then prefixForPositives + str else str
// NOTE: difference - for 'g' format we use isInt check to detect situations when '5.0' is printed as '5'
// in this case we need to override padding and always use ' ', otherwise we'll produce incorrect results
if isInteger then
str.PadRight(w, ' ') // don't pad integer numbers with '0' when 'g' format is specified (may yield incorrect results)
else
str.PadRight(w, padChar) // non-integer => string representation has point => can pad with any character
else
str.PadRight(w, ' ') // pad NaNs with ' '
let leftJustifyWithNonGFormat (str: string) isNumber isPositive w (prefixForPositives: string) padChar =
if isNumber then
let str = if isPositive then prefixForPositives + str else str
str.PadRight(w, padChar)
else
str.PadRight(w, ' ') // pad NaNs with ' '
/// processes given string based depending on values isNumber\isPositive
let noJustificationCore (str: string) isNumber isPositive prefixForPositives =
if isNumber && isPositive then prefixForPositives + str
else str
/// noJustification handler for f: 'T -> string - basic integer types
let noJustification (f: objnull -> string) (prefix: string) isUnsigned =
if isUnsigned then
fun (v: objnull) -> noJustificationCore (f v) true true prefix
else
fun (v: objnull) -> noJustificationCore (f v) true (isPositive v) prefix
/// contains functions to handle left/right and no justification case for numbers
module Integer =
let eliminateNative (v: objnull) =
match v with
| :? nativeint as n ->
if IntPtr.Size = 4 then box (n.ToInt32())
else box (n.ToInt64())
| :? unativeint as n ->
if IntPtr.Size = 4 then box (uint32 (n.ToUInt32()))
else box (uint64 (n.ToUInt64()))
| _ -> v
let rec toString (v: objnull) =
match v with
| :? int32 as n -> n.ToString(CultureInfo.InvariantCulture)
| :? int64 as n -> n.ToString(CultureInfo.InvariantCulture)
| :? sbyte as n -> n.ToString(CultureInfo.InvariantCulture)
| :? byte as n -> n.ToString(CultureInfo.InvariantCulture)
| :? int16 as n -> n.ToString(CultureInfo.InvariantCulture)
| :? uint16 as n -> n.ToString(CultureInfo.InvariantCulture)
| :? uint32 as n -> n.ToString(CultureInfo.InvariantCulture)
| :? uint64 as n -> n.ToString(CultureInfo.InvariantCulture)
| :? nativeint | :? unativeint -> toString (eliminateNative v)
| _ -> failwith "toString: unreachable"
let rec toFormattedString fmt (v: obj) =
match v with
| :? int32 as n -> n.ToString(fmt, CultureInfo.InvariantCulture)
| :? int64 as n -> n.ToString(fmt, CultureInfo.InvariantCulture)
| :? sbyte as n -> n.ToString(fmt, CultureInfo.InvariantCulture)
| :? byte as n -> n.ToString(fmt, CultureInfo.InvariantCulture)
| :? int16 as n -> n.ToString(fmt, CultureInfo.InvariantCulture)
| :? uint16 as n -> n.ToString(fmt, CultureInfo.InvariantCulture)
| :? uint32 as n -> n.ToString(fmt, CultureInfo.InvariantCulture)
| :? uint64 as n -> n.ToString(fmt, CultureInfo.InvariantCulture)
| :? nativeint | :? unativeint -> toFormattedString fmt (eliminateNative v)
| _ -> failwith "toFormattedString: unreachable"
let rec toUnsigned (v: objnull) =
match v with
| :? int32 as n -> box (uint32 n)
| :? int64 as n -> box (uint64 n)
| :? sbyte as n -> box (byte n)
| :? int16 as n -> box (uint16 n)
| :? nativeint | :? unativeint -> toUnsigned (eliminateNative v)
| _ -> v
/// Left justification handler for f: 'T -> string - basic integer types
let leftJustify isGFormat (f: objnull -> string) (prefix: string) padChar isUnsigned =
if isUnsigned then
if isGFormat then
fun (w: int) (v: objnull) ->
GenericNumber.leftJustifyWithGFormat (f v) true true true w prefix padChar
else
fun (w: int) (v: objnull) ->
GenericNumber.leftJustifyWithNonGFormat (f v) true true w prefix padChar
else
if isGFormat then
fun (w: int) (v: objnull) ->
GenericNumber.leftJustifyWithGFormat (f v) true true (GenericNumber.isPositive v) w prefix padChar
else
fun (w: int) (v: objnull) ->
GenericNumber.leftJustifyWithNonGFormat (f v) true (GenericNumber.isPositive v) w prefix padChar
/// Right justification handler for f: 'T -> string - basic integer types
let rightJustify f (prefixForPositives: string) padChar isUnsigned =
if isUnsigned then
if padChar = '0' then
fun (w: int) (v: objnull) ->
GenericNumber.rightJustifyWithZeroAsPadChar (f v) true true w prefixForPositives
else
System.Diagnostics.Debug.Assert((padChar = ' '))
fun (w: int) (v: objnull) ->
GenericNumber.rightJustifyWithSpaceAsPadChar (f v) true true w prefixForPositives
else
if padChar = '0' then
fun (w: int) (v: objnull) ->
GenericNumber.rightJustifyWithZeroAsPadChar (f v) true (GenericNumber.isPositive v) w prefixForPositives
else
System.Diagnostics.Debug.Assert((padChar = ' '))
fun (w: int) v ->
GenericNumber.rightJustifyWithSpaceAsPadChar (f v) true (GenericNumber.isPositive v) w prefixForPositives
/// Computes a new function from 'f' that wraps the basic conversion given
/// by 'f' with padding for 0, spacing and justification, if the flags specify
/// it. If they don't, f is made into a value converter
let withPadding (spec: FormatSpecifier) isUnsigned (f: objnull -> string) =
let allowZeroPadding = not (isLeftJustify spec.Flags) || spec.IsDecimalFormat
let padChar, prefix = spec.GetPadAndPrefix allowZeroPadding
Padding.withPadding spec
(GenericNumber.noJustification f prefix isUnsigned)
(leftJustify spec.IsGFormat f prefix padChar isUnsigned)
(rightJustify f prefix padChar isUnsigned)
let getValueConverter (spec: FormatSpecifier) : ValueConverter =
match spec.TypeChar with
| 'd' | 'i' ->
withPadding spec false toString
| 'u' ->
withPadding spec true (toUnsigned >> toString)
| 'x' ->
withPadding spec true (toFormattedString "x")
| 'X' ->
withPadding spec true (toFormattedString "X")
| 'o' ->
withPadding spec true (fun (v: objnull) ->
// Convert.ToInt64 throws for uint64 with values above int64 range so cast directly
match toUnsigned v with
| :? uint64 as u -> Convert.ToString(int64 u, 8)
| u -> Convert.ToString(Convert.ToInt64 u, 8))
| 'B' ->
withPadding spec true (fun (v: objnull) ->
match toUnsigned v with
| :? uint64 as u -> Convert.ToString(int64 u, 2)
| u -> Convert.ToString(Convert.ToInt64 u, 2))
| _ -> invalidArg (nameof spec) "Invalid integer format"
module FloatAndDecimal =
let fixupSign isPositive (nStr: string) =
// .NET Core and .NET Framework differ in how ToString and other formatting methods handle certain negative floating-point values (namely, -0.0 and values which round to -0.0 upon display).
// (see: https://devblogs.microsoft.com/dotnet/floating-point-parsing-and-formatting-improvements-in-net-core-3-0/)
// So in order for F#'s sprintf to behave consistently across platforms, we essentially "polyfill" (normalize) the output to ToString across the two runtimes. Specifically we do this by
// removing the '-' character in situations where the rest of the sprintf logic treats the number as positive, but .NET Core treats it as negative (i.e. -0.0, or -0.0000000001 when
// displaying with only a few decimal places)
// TODO: make this work for numbers like -0.0000000001
if isPositive && nStr.StartsWith "-" then
nStr.Substring 1
else
nStr
let rec toFormattedString fmt (v: obj) =
match v with
| :? single as n -> n.ToString(fmt, CultureInfo.InvariantCulture) |> fixupSign (GenericNumber.singleIsPositive n)
| :? double as n -> n.ToString(fmt, CultureInfo.InvariantCulture) |> fixupSign (GenericNumber.doubleIsPositive n)
| :? decimal as n -> n.ToString(fmt, CultureInfo.InvariantCulture) |> fixupSign (GenericNumber.decimalIsPositive n)
| _ -> failwith "toFormattedString: unreachable"
let isNumber (x: obj) =
match x with
| :? single as x ->
not (Single.IsPositiveInfinity(x)) &&
not (Single.IsNegativeInfinity(x)) &&
not (Single.IsNaN(x))
| :? double as x ->
not (Double.IsPositiveInfinity(x)) &&
not (Double.IsNegativeInfinity(x)) &&
not (Double.IsNaN(x))
| :? decimal -> true
| _ -> failwith "isNumber: unreachable"
let isInteger (n: obj) =
match n with
| :? single as n -> n % 1.0f = 0.0f
| :? double as n -> n % 1. = 0.
| :? decimal as n -> n % 1.0M = 0.0M
| _ -> failwith "isInteger: unreachable"
let noJustification (prefixForPositives: string) =
fun (fmt: string) (v: obj) ->
GenericNumber.noJustificationCore (toFormattedString fmt v) (isNumber v) (GenericNumber.isPositive v) prefixForPositives
let leftJustify isGFormat (prefix: string) padChar =
if isGFormat then
fun (fmt: string) (w: int) (v: obj) ->
GenericNumber.leftJustifyWithGFormat (toFormattedString fmt v) (isNumber v) (isInteger v) (GenericNumber.isPositive v) w prefix padChar
else
fun (fmt: string) (w: int) (v: obj) ->
GenericNumber.leftJustifyWithNonGFormat (toFormattedString fmt v) (isNumber v) (GenericNumber.isPositive v) w prefix padChar
let rightJustify (prefixForPositives: string) padChar =
if padChar = '0' then
fun (fmt: string) (w: int) (v: obj) ->
GenericNumber.rightJustifyWithZeroAsPadChar (toFormattedString fmt v) (isNumber v) (GenericNumber.isPositive v) w prefixForPositives
else
System.Diagnostics.Debug.Assert((padChar = ' '))
fun (fmt: string) (w: int) (v: obj) ->
GenericNumber.rightJustifyWithSpaceAsPadChar (toFormattedString fmt v) (isNumber v) (GenericNumber.isPositive v) w prefixForPositives
let withPadding (spec: FormatSpecifier) getFormat defaultFormat =
let padChar, prefix = spec.GetPadAndPrefix true
Padding.withPaddingFormatted spec getFormat defaultFormat
(noJustification prefix)
(leftJustify spec.IsGFormat prefix padChar)
(rightJustify prefix padChar)
type ObjectPrinter =
static member ObjectToString(spec: FormatSpecifier) : ValueConverter =
Basic.withPadding spec (fun (v: objnull) ->
match v with
| null -> "<null>"
| x -> x.ToString())
/// Convert an interpoland to a string
static member InterpolandToString(spec: FormatSpecifier) : ValueConverter =
let fmt =
match spec.InteropHoleDotNetFormat with
| ValueNone -> null
| ValueSome fmt -> "{0:" + fmt + "}"
Basic.withPadding spec (fun (vobj: objnull) ->
match vobj with
| null -> ""
| x ->
match fmt with
| null -> x.ToString()
| fmt -> String.Format(fmt, x))
static member GenericToStringCore(v: 'T, opts: Microsoft.FSharp.Text.StructuredPrintfImpl.FormatOptions, bindingFlags) =
let vty =
match box v with
| null -> typeof<'T>
| _ -> v.GetType()
Microsoft.FSharp.Text.StructuredPrintfImpl.Display.anyToStringForPrintf opts bindingFlags (v, vty)
static member GenericToString<'T>(spec: FormatSpecifier) : ValueConverter =
let bindingFlags =
if isPlusForPositives spec.Flags then BindingFlags.Public ||| BindingFlags.NonPublic
else BindingFlags.Public
let useZeroWidth = isPadWithZeros spec.Flags
let opts =
let o = Microsoft.FSharp.Text.StructuredPrintfImpl.FormatOptions.Default
let o =
if useZeroWidth then { o with PrintWidth = 0}
elif spec.IsWidthSpecified then { o with PrintWidth = spec.Width}
else o
if spec.IsPrecisionSpecified then { o with PrintSize = spec.Precision}
else o
match spec.IsStarWidth, spec.IsStarPrecision with
| true, true ->
ValueConverter.Make (fun (vobj: objnull) (width: int) (prec: int) ->
let v = unbox<'T> vobj
let opts = { opts with PrintSize = prec }
let opts = if not useZeroWidth then { opts with PrintWidth = width} else opts
ObjectPrinter.GenericToStringCore(v, opts, bindingFlags)
)
| true, false ->
ValueConverter.Make (fun (vobj: objnull) (width: int) ->
let v = unbox<'T> vobj
let opts = if not useZeroWidth then { opts with PrintWidth = width} else opts
ObjectPrinter.GenericToStringCore(v, opts, bindingFlags))
| false, true ->
ValueConverter.Make (fun (vobj: objnull) (prec: int) ->
let v = unbox<'T> vobj
let opts = { opts with PrintSize = prec }
ObjectPrinter.GenericToStringCore(v, opts, bindingFlags) )
| false, false ->
ValueConverter.Make (fun (vobj: objnull) ->
let v = unbox<'T> vobj
ObjectPrinter.GenericToStringCore(v, opts, bindingFlags))
let basicFloatToString spec =
let defaultFormat = getFormatForFloat spec.TypeChar DefaultPrecision
FloatAndDecimal.withPadding spec (getFormatForFloat spec.TypeChar) defaultFormat
let private AllStatics = BindingFlags.Public ||| BindingFlags.NonPublic ||| BindingFlags.Static