summaryrefslogtreecommitdiff
path: root/mcs/class/System/System.ComponentModel/MaskedTextProvider.cs
blob: bc9535941e77da41dd84d7359293156bfe383d7a (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
//
// Authors:
//	Rolf Bjarne Kvinge  <RKvinge@novell.com>
//
//

/*

Mask language:

0	Digit, required. This element will accept any single digit between 0 and 9.
9	Digit or space, optional. 
#	Digit or space, optional. If this position is blank in the mask, it will be rendered as a space in the Text property. Plus (+) and minus (-) signs are allowed.
L	Letter, required. Restricts input to the ASCII letters a-z and A-Z. This mask element is equivalent to [a-zA-Z] in regular expressions. 
?	Letter, optional. Restricts input to the ASCII letters a-z and A-Z. This mask element is equivalent to [a-zA-Z]? in regular expressions. 
&	Character, required. If the AsciiOnly property is set to true, this element behaves like the "L" element. 
C	Character, optional. Any non-control character. If the AsciiOnly property is set to true, this element behaves like the "?" element.
 * LAMESPEC: A is REQUIRED, not optional.
A	Alphanumeric, optional. If the AsciiOnly property is set to true, the only characters it will accept are the ASCII letters a-z and A-Z.
a	Alphanumeric, optional. If the AsciiOnly property is set to true, the only characters it will accept are the ASCII letters a-z and A-Z.
.	Decimal placeholder. The actual display character used will be the decimal symbol appropriate to the format provider, as determined by the control's FormatProvider property.
,	Thousands placeholder. The actual display character used will be the thousands placeholder appropriate to the format provider, as determined by the control's FormatProvider property.
:	Time separator. The actual display character used will be the time symbol appropriate to the format provider, as determined by the control's FormatProvider property.
/	Date separator. The actual display character used will be the date symbol appropriate to the format provider, as determined by the control's FormatProvider property.
$	Currency symbol. The actual character displayed will be the currency symbol appropriate to the format provider, as determined by the control's FormatProvider property.
<	Shift down. Converts all characters that follow to lowercase. 
> 	Shift up. Converts all characters that follow to uppercase.
|	Disable a previous shift up or shift down.
\	Escape. Escapes a mask character, turning it into a literal. "\\" is the escape sequence for a backslash.

 * */
 
using System.Globalization;
using System.Collections;
using System.Diagnostics;
using System.Text;

namespace System.ComponentModel {
	public class MaskedTextProvider : ICloneable
	{
#region Private fields
		private bool allow_prompt_as_input;
		private bool ascii_only;
		private CultureInfo culture; 
		private bool include_literals;
		private bool include_prompt;
		private bool is_password;
		private string mask;
		private char password_char;
		private char prompt_char;
		private bool reset_on_prompt;
		private bool reset_on_space;
		private bool skip_literals;
		
		private EditPosition [] edit_positions;
		
		const char default_prompt_char = '_';
		const char default_password_char = char.MinValue;
		
#endregion

#region Private classes
		private enum EditState {
			None,
			UpperCase,
			LowerCase
		}
		private enum EditType {
			DigitRequired, // 0
			DigitOrSpaceOptional, // 9
			DigitOrSpaceOptional_Blank, // #
			LetterRequired, // L
			LetterOptional, // ?
			CharacterRequired, // &
			CharacterOptional, // C
			AlphanumericRequired, // A
			AlphanumericOptional, // a
			DecimalPlaceholder, // .
			ThousandsPlaceholder, // ,
			TimeSeparator, // :
			DateSeparator, // /
			CurrencySymbol, // $ 
			Literal
		}

		private class EditPosition {
			public MaskedTextProvider Parent;
			public EditType Type;
			public EditState State;
			public char MaskCharacter;
			public char input;
			
			public void Reset ()
			{
				input = char.MinValue;
			}
			
			internal EditPosition Clone () {
				EditPosition result = new EditPosition ();
				result.Parent = Parent;
				result.Type = Type;
				result.State = State;
				result.MaskCharacter = MaskCharacter;
				result.input = input;
				return result;
			}
			
			public char Input {
				get {
					return input;
				}
				set {
					switch (State) {
					case EditState.LowerCase:
						input = char.ToLower (value, Parent.Culture);
						break;
					case EditState.UpperCase:
						input = char.ToUpper (value, Parent.Culture);
						break;
					default:// case EditState.None:
						input = value;
						break;
					}
				}
			}
			
			public bool IsAscii (char c)
			{
				return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
			}
			
			public bool Match (char c, out MaskedTextResultHint resultHint, bool only_test)
			{
				if (!MaskedTextProvider.IsValidInputChar (c)) {
					resultHint = MaskedTextResultHint.InvalidInput;
					return false;
				}

				if (Parent.ResetOnSpace && c == ' ' && Editable) {
					resultHint = MaskedTextResultHint.CharacterEscaped;
					if (FilledIn) {
						resultHint = MaskedTextResultHint.Success;
						if (!only_test && input != ' ') {
							switch (Type) {
							case EditType.AlphanumericOptional:
							case EditType.AlphanumericRequired:
							case EditType.CharacterOptional:
							case EditType.CharacterRequired:
								Input = c;
								break;
							default:
								Input = char.MinValue;
								break;
							}
						}
					}
					return true;
				}
				
				if (Type == EditType.Literal && MaskCharacter == c && Parent.SkipLiterals) {
					resultHint = MaskedTextResultHint.Success;
					return true;
				}
				
				if (!Editable) {
					resultHint = MaskedTextResultHint.NonEditPosition;
					return false;
				}

				switch (Type) {
					case EditType.AlphanumericOptional:
					case EditType.AlphanumericRequired:
						if (char.IsLetterOrDigit (c)) {
							if (Parent.AsciiOnly && !IsAscii (c)) {
								resultHint = MaskedTextResultHint.AsciiCharacterExpected;
								return false;
							} else {
								if (!only_test) {
									Input = c;
								}
								resultHint = MaskedTextResultHint.Success;
								return true;
							}
						} else {
							resultHint = MaskedTextResultHint.AlphanumericCharacterExpected;
							return false;
						}
					case EditType.CharacterOptional:
					case EditType.CharacterRequired:
						if (Parent.AsciiOnly && !IsAscii (c)) {
							resultHint = MaskedTextResultHint.LetterExpected;
							return false;
						} else if (!char.IsControl (c)) {
							if (!only_test) {
								Input = c;
							}
							resultHint = MaskedTextResultHint.Success;
							return true;
						} else {
							resultHint = MaskedTextResultHint.LetterExpected;
							return false;
						}
					case EditType.DigitOrSpaceOptional:
					case EditType.DigitOrSpaceOptional_Blank:
						if (char.IsDigit (c) || c == ' ') {
							if (!only_test) {
								Input = c;
							}
							resultHint = MaskedTextResultHint.Success;
							return true;
						} else {
							resultHint = MaskedTextResultHint.DigitExpected;
							return false;
						}
					case EditType.DigitRequired:
						if (char.IsDigit (c)) {
							if (!only_test) {
								Input = c;
							}
							resultHint = MaskedTextResultHint.Success;
							return true;
						} else {
							resultHint = MaskedTextResultHint.DigitExpected;
							return false;
						}
					case EditType.LetterOptional:
					case EditType.LetterRequired:
						if (!char.IsLetter (c)) {
							resultHint = MaskedTextResultHint.LetterExpected;
							return false;
						} else if (Parent.AsciiOnly && !IsAscii (c)) {
							resultHint = MaskedTextResultHint.LetterExpected;
							return false;
						} else {
							if (!only_test) {
								Input = c;
							}
							resultHint = MaskedTextResultHint.Success;
							return true;
						}
					default:
						resultHint = MaskedTextResultHint.Unknown;
						return false;
				}
			}
			
			public bool FilledIn {
				get {
					return Input != char.MinValue;
				}
			}
			
			public bool Required {
				get {
					switch (MaskCharacter) {
					case '0':
					case 'L':
					case '&':
					case 'A':
						return true;
					default:
						return false;
					}
				}
			}
			
			public bool Editable {
				get {
					switch (MaskCharacter) {
					case '0':
					case '9':
					case '#':
					case 'L':
					case '?':
					case '&':
					case 'C':
					case 'A':
					case 'a':
						return true;
					default:
						return false;
					}
				}
			}	
			
			public bool Visible {
				get {
					switch (MaskCharacter) {
					case '|':
					case '<':
					case '>':
						return false;
					default:
						return true;
					}
				}
			}
			public string Text {
				get {
					if (Type == EditType.Literal) {
						return MaskCharacter.ToString ();
					}
					switch (MaskCharacter) {
					case '.': return Parent.Culture.NumberFormat.NumberDecimalSeparator;
					case ',': return Parent.Culture.NumberFormat.NumberGroupSeparator;
					case ':': return Parent.Culture.DateTimeFormat.TimeSeparator;
					case '/': return Parent.Culture.DateTimeFormat.DateSeparator;
					case '$': return Parent.Culture.NumberFormat.CurrencySymbol;
					default:
						return FilledIn ? Input.ToString () : Parent.PromptChar.ToString ();
					}
				}
			}

			private EditPosition ()
			{
			}

			public EditPosition (MaskedTextProvider Parent, EditType Type, EditState State, char MaskCharacter)
			{
				this.Type = Type;
				this.Parent = Parent;
				this.State = State;
				this.MaskCharacter = MaskCharacter;
				
			}
		}
#endregion

#region Private methods & properties
		private void SetMask (string mask)
		{
			if (mask == null || mask == string.Empty)
				throw new ArgumentException ("The Mask value cannot be null or empty.\r\nParameter name: mask");

			this.mask = mask;
			
			System.Collections.Generic.List <EditPosition> list = new System.Collections.Generic.List<EditPosition> (mask.Length);
			
			EditState State = EditState.None;
			bool escaped = false;
			for (int i = 0; i < mask.Length; i++) {
				if (escaped) {
					list.Add (new EditPosition (this, EditType.Literal, State, mask [i]));
					escaped = false;
					continue;
				}
				
				switch (mask [i]) {
				case '\\':
					escaped = true; 
					break;
				case '0':
					list.Add (new EditPosition (this, EditType.DigitRequired, State, mask [i])); 
					break;
				case '9':
					list.Add (new EditPosition (this, EditType.DigitOrSpaceOptional, State, mask [i]));
					break;
				case '#':
					list.Add (new EditPosition (this, EditType.DigitOrSpaceOptional_Blank, State, mask [i]));
					break;
				case 'L':
					list.Add (new EditPosition (this, EditType.LetterRequired, State, mask [i]));
					break;
				case '?':
					list.Add (new EditPosition (this, EditType.LetterOptional, State, mask [i]));
					break;
				case '&':
					list.Add (new EditPosition (this, EditType.CharacterRequired, State, mask [i]));
					break;
				case 'C':
					list.Add (new EditPosition (this, EditType.CharacterOptional, State, mask [i]));
					break;
				case 'A':
					list.Add (new EditPosition (this, EditType.AlphanumericRequired, State, mask [i]));
					break;
				case 'a':
					list.Add (new EditPosition (this, EditType.AlphanumericOptional, State, mask [i]));
					break;
				case '.':
					list.Add (new EditPosition (this, EditType.DecimalPlaceholder, State, mask [i]));
					break;
				case ',':
					list.Add (new EditPosition (this, EditType.ThousandsPlaceholder, State, mask [i]));
					break;
				case ':':
					list.Add (new EditPosition (this, EditType.TimeSeparator, State, mask [i]));
					break;
				case '/':
					list.Add (new EditPosition (this, EditType.DateSeparator, State, mask [i]));
					break;
				case '$':
					list.Add (new EditPosition (this, EditType.CurrencySymbol, State, mask [i]));
					break;
				case '<':
					State = EditState.LowerCase;
					break;
				case '>':
					State = EditState.UpperCase;
					break;
				case '|':
					State = EditState.None;
					break;
				default:
					list.Add (new EditPosition (this, EditType.Literal, State, mask [i]));
					break;
				}
			}
			edit_positions = list.ToArray ();
		}
		
		private EditPosition [] ClonePositions ()
		{
			EditPosition [] result = new EditPosition [edit_positions.Length];
			for (int i = 0; i < result.Length; i++) {
				result [i] = edit_positions [i].Clone ();
			}
			return result;
		}

		private bool AddInternal (string str_input, out int testPosition, out MaskedTextResultHint resultHint, bool only_test)
		{
			EditPosition [] edit_positions;
			
			if (only_test) {
				edit_positions = ClonePositions ();
			} else {
				edit_positions = this.edit_positions;
			}

			if (str_input == null)
				throw new ArgumentNullException ("input");

			if (str_input.Length == 0) {
				resultHint = MaskedTextResultHint.NoEffect;
				testPosition = LastAssignedPosition + 1;
				return true;
			}

			resultHint = MaskedTextResultHint.Unknown;
			testPosition = 0;

			int next_position = LastAssignedPosition;
			MaskedTextResultHint tmpResult = MaskedTextResultHint.Unknown;

			if (next_position >= edit_positions.Length) {
				testPosition = next_position;
				resultHint = MaskedTextResultHint.UnavailableEditPosition;
				return false;
			}

			for (int i = 0; i < str_input.Length; i++) {
				char input = str_input [i];
				next_position++;
				testPosition = next_position;
				
				if (tmpResult > resultHint) {
					resultHint = tmpResult;
				}

				if (VerifyEscapeChar (input, next_position)) {
					tmpResult = MaskedTextResultHint.CharacterEscaped;
					continue;
				}

				next_position = FindEditPositionFrom (next_position, true);
				testPosition = next_position;

				if (next_position == InvalidIndex) {
					testPosition = edit_positions.Length;
					resultHint = MaskedTextResultHint.UnavailableEditPosition;
					return false;
				}

				if (!IsValidInputChar (input)) {
					testPosition = next_position;
					resultHint = MaskedTextResultHint.InvalidInput;
					return false;
				}
				
				if (!edit_positions [next_position].Match (input, out tmpResult, false)) {
					testPosition = next_position;
					resultHint = tmpResult;
					return false;
				}		
			}

			if (tmpResult > resultHint) {
				resultHint = tmpResult;
			}

			return true;
		}
		
		private bool AddInternal (char input, out int testPosition, out MaskedTextResultHint resultHint, bool check_available_positions_first, bool check_escape_char_first)
		{
			/*
			 * check_available_positions_first: when adding a char, MS first seems to check if there are any available edit positions, then if there are any
			 * they will try to add the char (meaning that if you add a char matching a literal char in the mask with SkipLiterals and there are no
			 * more available edit positions, it will fail).
			 * 
			 * check_escape_char_first: When adding a char, MS doesn't check for escape char, they directly search for the first edit position.
			 * When adding a string, they check first for escape char, then if not successful they find the first edit position.
			 */
			 
			int new_position;
			testPosition = 0;
			
			new_position = LastAssignedPosition + 1;

			if (check_available_positions_first) {
				int tmp = new_position;
				bool any_available = false;
				while (tmp < edit_positions.Length) {
					if (edit_positions [tmp].Editable) {
						any_available = true;
						break;
					}
					tmp++;
				}
				if (!any_available) {
					testPosition = tmp;
					resultHint = MaskedTextResultHint.UnavailableEditPosition;
					return GetOperationResultFromHint (resultHint);
				}
			}

			if (check_escape_char_first) {
				if (VerifyEscapeChar (input, new_position)) {
					testPosition = new_position;
					resultHint = MaskedTextResultHint.CharacterEscaped;
					return true;
				}
			}

			new_position = FindEditPositionFrom (new_position, true);

			if (new_position > edit_positions.Length - 1 ||new_position == InvalidIndex) {
				testPosition = new_position;
				resultHint = MaskedTextResultHint.UnavailableEditPosition;
				return GetOperationResultFromHint (resultHint);
			}

			if (!IsValidInputChar (input)) {
				testPosition = new_position;
				resultHint = MaskedTextResultHint.InvalidInput;
				return GetOperationResultFromHint (resultHint);
			}

			if (!edit_positions [new_position].Match (input, out resultHint, false)) {
				testPosition = new_position;
				return GetOperationResultFromHint (resultHint);
			}

			testPosition = new_position;

			return GetOperationResultFromHint (resultHint);
		}
		
		private bool VerifyStringInternal (string input, out int testPosition, out MaskedTextResultHint resultHint, int startIndex, bool only_test)
		{
			int previous_position = startIndex;
			int current_position;
			resultHint = MaskedTextResultHint.Unknown;
			
			// Replace existing characters
			for (int i = 0; i < input.Length; i++) {
				MaskedTextResultHint tmpResult;
				current_position = FindEditPositionFrom (previous_position, true);
				if (current_position == InvalidIndex) {
					testPosition = edit_positions.Length;
					resultHint = MaskedTextResultHint.UnavailableEditPosition;
					return false;
				}
				
				if (!VerifyCharInternal (input [i], current_position, out tmpResult, only_test)) {
					testPosition = current_position;
					resultHint = tmpResult;
					return false;
				}
				if (tmpResult > resultHint) {
					resultHint = tmpResult;
				}
				previous_position = current_position + 1;
				
			}
			// Remove characters not in the input.
			if (!only_test) {
				previous_position = FindEditPositionFrom (previous_position, true);
				while (previous_position != InvalidIndex) {
					if (edit_positions [previous_position].FilledIn) {
						edit_positions [previous_position].Reset ();
						if (resultHint != MaskedTextResultHint.NoEffect) {
							resultHint = MaskedTextResultHint.Success;
						}
					}
					previous_position = FindEditPositionFrom (previous_position + 1, true);
				}
			}
			if (input.Length > 0) {
				testPosition = startIndex + input.Length - 1;
			} else {
				testPosition = startIndex;
				if (resultHint < MaskedTextResultHint.NoEffect) {
					resultHint = MaskedTextResultHint.NoEffect;
				}
			}
			
			return true;
		}

		private bool VerifyCharInternal (char input, int position, out MaskedTextResultHint hint, bool only_test)
		{
			hint = MaskedTextResultHint.Unknown;

			if (position < 0 || position >= edit_positions.Length) {
				hint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}

			if (!IsValidInputChar (input)) {
				hint = MaskedTextResultHint.InvalidInput;
				return false;
			}

			if (input == ' ' && ResetOnSpace && edit_positions [position].Editable && edit_positions [position].FilledIn) {
				if (!only_test) {
					edit_positions [position].Reset ();
				}
				hint = MaskedTextResultHint.SideEffect;
				return true;
			}
			
			if (edit_positions [position].Editable &&  edit_positions [position].FilledIn && edit_positions [position].input == input) {
				hint = MaskedTextResultHint.NoEffect;
				return true;
			}

			if (SkipLiterals && !edit_positions [position].Editable && edit_positions [position].Text == input.ToString ()) {
				hint = MaskedTextResultHint.CharacterEscaped;
				return true;
			}

			return edit_positions [position].Match (input, out hint, only_test);
		}
		
		// Test to see if the input string can be inserted at the specified position.
		// Does not try to move characters.
		private bool IsInsertableString (string str_input, int position, out int testPosition, out MaskedTextResultHint resultHint)
		{
			int current_position = position;
			int test_position;
			
			resultHint = MaskedTextResultHint.UnavailableEditPosition;
			testPosition = InvalidIndex;
			
			for (int i = 0; i < str_input.Length; i++) {
				char ch = str_input [i];

				test_position = FindEditPositionFrom (current_position, true);
				
				if (test_position != InvalidIndex && VerifyEscapeChar (ch, test_position)) {
					current_position = test_position + 1;
					continue;
				}
				
				if (VerifyEscapeChar (ch, current_position)) {
					current_position = current_position + 1;
					continue;
				}
				
				
				if (test_position == InvalidIndex) {
					resultHint = MaskedTextResultHint.UnavailableEditPosition;
					testPosition = edit_positions.Length;
					return false;
				}

				testPosition = test_position;
				
				if (!edit_positions [test_position].Match (ch, out resultHint, true)) {
					return false;
				}
				
				current_position = test_position + 1;
			}
			resultHint = MaskedTextResultHint.Success;
			
			return true;
		}
		
		private bool ShiftPositionsRight (EditPosition [] edit_positions, int start, out int testPosition, out MaskedTextResultHint resultHint)
		{
			int index = start;
			int last_assigned_index = FindAssignedEditPositionFrom (edit_positions.Length, false);
			int endindex = FindUnassignedEditPositionFrom (last_assigned_index, true); // Find the first non-assigned edit position
			
			testPosition = start;
			resultHint = MaskedTextResultHint.Unknown;

			if (endindex == InvalidIndex) {
				// No more free edit positions.
				testPosition = edit_positions.Length;
				resultHint = MaskedTextResultHint.UnavailableEditPosition;
				return false;
			}
		
			while (endindex > index) {
				char char_to_assign;
				int index_to_move;
				
				index_to_move = FindEditPositionFrom (endindex - 1, false);	
				char_to_assign = edit_positions [index_to_move].input;

				if (char_to_assign == char.MinValue) {
					edit_positions [endindex].input = char_to_assign;
				} else {
					if (!edit_positions [endindex].Match (char_to_assign, out resultHint, false)) {
						testPosition = endindex;
						return false;
					}
				}
				endindex = index_to_move;
			}
			
			if (endindex != InvalidIndex) {
				edit_positions [endindex].Reset ();
			}
			
			return true;
		}
		
		private bool ReplaceInternal (string input, int startPosition, int endPosition, out int testPosition, out MaskedTextResultHint resultHint, bool only_test, bool dont_remove_at_end)
		{
			EditPosition [] edit_positions;
			resultHint = MaskedTextResultHint.Unknown;
			
			if (only_test) {
				edit_positions = ClonePositions ();
			} else {
				edit_positions = this.edit_positions;
			}
			
			if (input == null)
				throw new ArgumentNullException ("input");

			if (endPosition >= edit_positions.Length) {
				testPosition = endPosition;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}
			
			if (startPosition < 0) {
				testPosition = startPosition;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}
			
			if (startPosition >= edit_positions.Length) {
				testPosition = startPosition;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}
			
			if (startPosition > endPosition) {
				testPosition = startPosition;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}

			
			if (input.Length == 0) {
				return RemoveAtInternal (startPosition, endPosition, out testPosition, out resultHint, only_test);
			}

			int previous_position = startPosition;
			int current_position = previous_position;
			MaskedTextResultHint tmpResult = MaskedTextResultHint.Unknown;
			testPosition = InvalidIndex;
			
			for (int i = 0; i < input.Length; i++) {
				char current_input = input [i];
				
				current_position = previous_position;

				if (VerifyEscapeChar (current_input, current_position)) {
					if (edit_positions [current_position].FilledIn &&
						edit_positions [current_position].Editable &&
						(current_input == ' ' && ResetOnSpace) || (current_input == PromptChar && ResetOnPrompt)) {
						edit_positions [current_position].Reset ();
						tmpResult = MaskedTextResultHint.SideEffect;
					} else {
						tmpResult = MaskedTextResultHint.CharacterEscaped;
					}
				} else if (current_position < edit_positions.Length && !edit_positions [current_position].Editable && FindAssignedEditPositionInRange (current_position, endPosition, true) == InvalidIndex) {
					// If replacing over a literal, the replacement character is INSERTED at the next
					// available edit position. Weird, huh?
					current_position = FindEditPositionFrom (current_position, true);
					if (current_position == InvalidIndex) {
						resultHint = MaskedTextResultHint.UnavailableEditPosition;
						testPosition = edit_positions.Length;
						return false;
					}
					if (!InsertAtInternal (current_input.ToString (), current_position, out testPosition, out tmpResult, only_test)) {
						resultHint = tmpResult;
						return false;
					}
				} else { 
				
					current_position = FindEditPositionFrom (current_position, true);

					if (current_position == InvalidIndex) {
						testPosition = edit_positions.Length;
						resultHint = MaskedTextResultHint.UnavailableEditPosition;
						return false;
					}

					if (!IsValidInputChar (current_input)) {
						testPosition = current_position;
						resultHint = MaskedTextResultHint.InvalidInput;
						return false;
					}

					if (!ReplaceInternal (edit_positions, current_input, current_position, out testPosition, out tmpResult, false)) {
						resultHint = tmpResult;
						return false;
					}
				}
				
				if (tmpResult > resultHint) {
					resultHint = tmpResult;
				}
				
				previous_position = current_position + 1;
			}

			testPosition = current_position;
			
			int tmpPosition;
			if (!dont_remove_at_end && previous_position <= endPosition) {
				if (!RemoveAtInternal (previous_position, endPosition, out tmpPosition, out tmpResult, only_test)) {
					testPosition = tmpPosition;
					resultHint = tmpResult;
					return false;
				}
			}
			if (tmpResult == MaskedTextResultHint.Success && resultHint < MaskedTextResultHint.SideEffect) {
				resultHint = MaskedTextResultHint.SideEffect;
			}
				
			return true;
		}
		
		private bool ReplaceInternal (EditPosition [] edit_positions, char input, int position, out int testPosition, out MaskedTextResultHint resultHint, bool only_test)
		{
			testPosition = position;

			if (!IsValidInputChar (input)) {
				resultHint = MaskedTextResultHint.InvalidInput;
				return false;
			}

			if (VerifyEscapeChar (input, position)) {
				if (edit_positions [position].FilledIn && edit_positions [position].Editable && (input == ' ' && ResetOnSpace) || (input == PromptChar && ResetOnPrompt)) {
					edit_positions [position].Reset ();
					resultHint = MaskedTextResultHint.SideEffect;
				} else {
					resultHint = MaskedTextResultHint.CharacterEscaped;
				}
				testPosition = position;
				return true;
			}

			if (!edit_positions [position].Editable) {
				resultHint = MaskedTextResultHint.NonEditPosition;
				return false;
			}
			
			bool is_filled = edit_positions [position].FilledIn;
			if (is_filled && edit_positions [position].input == input) {
				if (VerifyEscapeChar (input, position)) {
					resultHint = MaskedTextResultHint.CharacterEscaped;
				} else {
					resultHint = MaskedTextResultHint.NoEffect;
				}
			} else if (input == ' ' && this.ResetOnSpace) {
				if (is_filled) {
					resultHint = MaskedTextResultHint.SideEffect;
					edit_positions [position].Reset ();
				} else {
					resultHint = MaskedTextResultHint.CharacterEscaped;
				}
				return true;
			} else if (VerifyEscapeChar (input, position)) {
				resultHint = MaskedTextResultHint.SideEffect;
			} else {
				resultHint = MaskedTextResultHint.Success;
			}
			MaskedTextResultHint tmpResult;
			if (!edit_positions [position].Match (input, out tmpResult, false)) {
				resultHint = tmpResult;
				return false;
			}

			return true;
		}

		private bool RemoveAtInternal (int startPosition, int endPosition, out int testPosition, out MaskedTextResultHint resultHint, bool only_testing)
		{
			EditPosition [] edit_positions;
			testPosition = -1;
			resultHint = MaskedTextResultHint.Unknown;

			if (only_testing) {
				edit_positions = ClonePositions ();
			} else {
				edit_positions = this.edit_positions;
			}

			if (endPosition < 0 || endPosition >= edit_positions.Length) {
				testPosition = endPosition;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}
			
			if (startPosition < 0 || startPosition >= edit_positions.Length) {
				testPosition = startPosition;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}
			
			
			if (startPosition > endPosition) {
				testPosition = startPosition;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}
			int edit_positions_in_range = 0;
			for (int i = startPosition; i <= endPosition; i++) {
				if (edit_positions [i].Editable) {
					edit_positions_in_range++;
				}
			}
			
			if (edit_positions_in_range == 0) {
				testPosition = startPosition;
				resultHint = MaskedTextResultHint.NoEffect;
				return true;
			}
			
			int current_edit_position = FindEditPositionFrom (startPosition, true);
			while (current_edit_position != InvalidIndex) {
				// Find the edit position that will reach the current position.
				int next_index = FindEditPositionFrom (current_edit_position + 1, true);
				for (int j = 1; j < edit_positions_in_range && next_index != InvalidIndex; j++) {
					next_index = FindEditPositionFrom (next_index + 1, true);
				}
				
				if (next_index == InvalidIndex) {
					if (edit_positions [current_edit_position].FilledIn) {
						edit_positions [current_edit_position].Reset ();
						resultHint = MaskedTextResultHint.Success;	
					} else {
						if (resultHint < MaskedTextResultHint.NoEffect) {
							resultHint = MaskedTextResultHint.NoEffect;
						}
					}
				} else {
					if (!edit_positions [next_index].FilledIn) {
						if (edit_positions [current_edit_position].FilledIn) {
							edit_positions [current_edit_position].Reset ();
							resultHint = MaskedTextResultHint.Success;	
						} else {
							if (resultHint < MaskedTextResultHint.NoEffect) {
								resultHint = MaskedTextResultHint.NoEffect;
							}
						}
					} else {
						MaskedTextResultHint tmpResult = MaskedTextResultHint.Unknown;
						if (edit_positions [current_edit_position].FilledIn) {
							resultHint = MaskedTextResultHint.Success;
						} else if (resultHint < MaskedTextResultHint.SideEffect) {
							resultHint = MaskedTextResultHint.SideEffect;
						}
						if (!edit_positions [current_edit_position].Match (edit_positions [next_index].input, out tmpResult, false)) {
							resultHint = tmpResult;
							testPosition = current_edit_position;
							return false;
						}
					}	
					edit_positions [next_index].Reset ();
				}
				current_edit_position = FindEditPositionFrom (current_edit_position + 1, true);
			}
			
			if (resultHint == MaskedTextResultHint.Unknown) {
				resultHint = MaskedTextResultHint.NoEffect;
			}
			
			testPosition = startPosition;
			
			return true;
		}
		
		private bool InsertAtInternal (string str_input, int position, out int testPosition, out MaskedTextResultHint resultHint, bool only_testing)
		{
			EditPosition [] edit_positions;
			testPosition = -1;
			resultHint = MaskedTextResultHint.Unknown;

			if (only_testing) {
				edit_positions = ClonePositions ();
			} else {
				edit_positions = this.edit_positions;
			}

			if (position < 0 || position >= edit_positions.Length) {
				testPosition = 0;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}
			
			if (!IsInsertableString (str_input, position, out testPosition, out resultHint)) {
				return false;
			}
			
			resultHint = MaskedTextResultHint.Unknown;
			
			int next_position = position;
			for (int i = 0; i < str_input.Length; i++) {
				char input = str_input [i];
				int index = FindEditPositionFrom (next_position, true); // Find the first edit position (here the input will go)
				int endindex = FindUnassignedEditPositionFrom (next_position, true); // Find the first non-assigned edit position
				bool escaped = false;

				if (VerifyEscapeChar (input, next_position)) {
					escaped = true;

					if (input.ToString () == edit_positions [next_position].Text) {
						if (FindAssignedEditPositionInRange (0, next_position - 1, true) != InvalidIndex && endindex == InvalidIndex) {
							resultHint = MaskedTextResultHint.UnavailableEditPosition;
							testPosition = edit_positions.Length;
							return false;
						}
						resultHint = MaskedTextResultHint.CharacterEscaped;
						testPosition = next_position;
						next_position++;
						continue;						
					}
				}
				
				if (!escaped && index == InvalidIndex) {
					// No edit positions left at all in the string
					testPosition = edit_positions.Length;
					resultHint = MaskedTextResultHint.UnavailableEditPosition;
					return false;
				}
				
				if (index == InvalidIndex) {
					index = next_position;
				}
				
				bool was_filled = edit_positions [index].FilledIn;
				bool shift = was_filled;
				if (shift) {
					if (!ShiftPositionsRight (edit_positions, index, out testPosition, out resultHint)) {
						return false;
					}
				}

				testPosition = index;
				if (escaped) {
					if (was_filled) {
						resultHint = MaskedTextResultHint.Success;
					} else if (!edit_positions [index].Editable && input.ToString () == edit_positions [index].Text) {
						resultHint = MaskedTextResultHint.CharacterEscaped;
						testPosition = next_position;
					} else {
						int first_edit_position = FindEditPositionFrom (index, true);
						if (first_edit_position == InvalidIndex) {
							resultHint = MaskedTextResultHint.UnavailableEditPosition;
							testPosition = edit_positions.Length;
							return false;
						}
			
						resultHint = MaskedTextResultHint.CharacterEscaped;
						if (input.ToString () == edit_positions [next_position].Text) {
							testPosition = next_position;
						}
					}
				} else {
					MaskedTextResultHint tmpResult;
					if (!edit_positions [index].Match (input, out tmpResult, false)) {
						resultHint = tmpResult;
						return false;
					}
					if (resultHint < tmpResult) {
						resultHint = tmpResult;
					}
				}
				next_position = index + 1;
				
			}
			
			return true;
		}
#endregion
		#region Public constructors
		static MaskedTextProvider()
		{
		}

		public MaskedTextProvider(string mask) 
			: this (mask, null, true, default_prompt_char, default_password_char, false)
		{
		}

		public MaskedTextProvider (string mask, bool restrictToAscii) 
			: this (mask, null, true, default_prompt_char, default_password_char, restrictToAscii)
		{
		}

		public MaskedTextProvider (string mask, CultureInfo culture) 
			: this (mask, culture, true, default_prompt_char, default_password_char, false)
		{
		}

		public MaskedTextProvider (string mask, char passwordChar, bool allowPromptAsInput) 
			: this (mask, null, allowPromptAsInput, default_prompt_char, passwordChar, false)
		{
		}

		public MaskedTextProvider (string mask, CultureInfo culture, bool restrictToAscii) 
			: this (mask, culture, true, default_prompt_char, default_password_char, restrictToAscii)
		{
		}
		
		public MaskedTextProvider(string mask, CultureInfo culture, char passwordChar, bool allowPromptAsInput) 
			: this (mask, culture, allowPromptAsInput, default_prompt_char, passwordChar, false)
		{
		}
		
		public MaskedTextProvider(string mask, CultureInfo culture, bool allowPromptAsInput, char promptChar, char passwordChar, bool restrictToAscii)
		{
			SetMask (mask);
			
			if (culture == null)
				this.culture = Threading.Thread.CurrentThread.CurrentCulture;
			else
				this.culture = culture;
				
			this.allow_prompt_as_input = allowPromptAsInput;
			
			this.PromptChar = promptChar;
			this.PasswordChar = passwordChar;
			this.ascii_only = restrictToAscii;
			
			include_literals = true;
			reset_on_prompt = true;
			reset_on_space = true;
			skip_literals = true;
		}
#endregion

#region Public methods
		public bool Add(char input)
		{
			int testPosition;
			MaskedTextResultHint resultHint;
			return Add (input, out testPosition, out resultHint);
		}

		public bool Add (string input)
		{
			int testPosition;
			MaskedTextResultHint resultHint;
			return Add (input, out testPosition, out resultHint);
		}

		public bool Add (char input, out int testPosition, out MaskedTextResultHint resultHint)
		{
			return AddInternal (input, out testPosition, out resultHint, true, false);
		}

		public bool Add (string input, out int testPosition, out MaskedTextResultHint resultHint)
		{
			bool result;
			
			result = AddInternal (input, out testPosition, out resultHint, true);
			
			if (result) {
				result = AddInternal (input, out testPosition, out resultHint, false);
			}
			
			return result;
		}

		public void Clear ()
		{
			MaskedTextResultHint resultHint;
			Clear (out resultHint);
		}

		public void Clear (out MaskedTextResultHint resultHint)
		{
			resultHint = MaskedTextResultHint.NoEffect;
			for (int i = 0; i < edit_positions.Length; i++) {
				if (edit_positions [i].Editable && edit_positions [i].FilledIn) {
					edit_positions [i].Reset ();
					resultHint = MaskedTextResultHint.Success;
				}
			}
		}

		public object Clone ()
		{
			MaskedTextProvider result = new MaskedTextProvider (mask);
			
			result.allow_prompt_as_input = allow_prompt_as_input;
			result.ascii_only = ascii_only;
			result.culture = culture;
			result.edit_positions = ClonePositions ();
			result.include_literals = include_literals;
			result.include_prompt = include_prompt;
			result.is_password = is_password;
			result.mask = mask;
			result.password_char = password_char;
			result.prompt_char = prompt_char;
			result.reset_on_prompt = reset_on_prompt;
			result.reset_on_space = reset_on_space;
			result.skip_literals = skip_literals;
						
			return result;
		}

		public int FindAssignedEditPositionFrom (int position, bool direction)
		{
			if (direction) {
				return FindAssignedEditPositionInRange (position, edit_positions.Length - 1, direction);
			} else {
				return FindAssignedEditPositionInRange (0, position, direction);			
			}
		}

		public int FindAssignedEditPositionInRange (int startPosition, int endPosition, bool direction)
		{
			int step;
			int start, end;
			
			if (startPosition < 0)
				startPosition = 0;
			if (endPosition >= edit_positions.Length)
				endPosition = edit_positions.Length - 1;

			if (startPosition > endPosition)
				return InvalidIndex;
				
			step = direction ? 1 : -1;
			start = direction ? startPosition : endPosition;
			end = (direction ? endPosition: startPosition) + step;

			for (int i = start; i != end; i+=step) {
				if (edit_positions [i].Editable && edit_positions [i].FilledIn)
					return i;
			}
			return InvalidIndex;
		}

		public int FindEditPositionFrom (int position, bool direction)
		{
			if (direction) {
				return FindEditPositionInRange (position, edit_positions.Length - 1, direction);
			} else {
				return FindEditPositionInRange (0, position, direction);
			}
		}

		public int FindEditPositionInRange (int startPosition, int endPosition, bool direction)
		{
			int step;
			int start, end;

			if (startPosition < 0)
				startPosition = 0;
			if (endPosition >= edit_positions.Length)
				endPosition = edit_positions.Length - 1;

			if (startPosition > endPosition)
				return InvalidIndex;

			step = direction ? 1 : -1;
			start = direction ? startPosition : endPosition;
			end = (direction ? endPosition : startPosition) + step;

			for (int i = start; i != end; i += step) {
				if (edit_positions [i].Editable)
					return i;
			}
			return InvalidIndex;
		}

		public int FindNonEditPositionFrom (int position, bool direction)
		{
			if (direction) {
				return FindNonEditPositionInRange (position, edit_positions.Length - 1, direction);
			} else {
				return FindNonEditPositionInRange (0, position, direction);
			}
		}

		public int FindNonEditPositionInRange (int startPosition, int endPosition, bool direction)
		{		
			int step;
			int start, end;

			if (startPosition < 0)
				startPosition = 0;
			if (endPosition >= edit_positions.Length)
				endPosition = edit_positions.Length - 1;

			if (startPosition > endPosition)
				return InvalidIndex;

			step = direction ? 1 : -1;
			start = direction ? startPosition : endPosition;
			end = (direction ? endPosition : startPosition) + step;

			for (int i = start; i != end; i += step) {
				if (!edit_positions [i].Editable)
					return i;
			}
			return InvalidIndex;
		}

		public int FindUnassignedEditPositionFrom (int position, bool direction)
		{
			if (direction) {
				return FindUnassignedEditPositionInRange (position, edit_positions.Length - 1, direction);
			} else {
				return FindUnassignedEditPositionInRange (0, position, direction);
			}
		}

		public int FindUnassignedEditPositionInRange (int startPosition, int endPosition, bool direction)
		{
			int step;
			int start, end;

			if (startPosition < 0)
				startPosition = 0;
			if (endPosition >= edit_positions.Length)
				endPosition = edit_positions.Length - 1;

			if (startPosition > endPosition)
				return InvalidIndex;

			step = direction ? 1 : -1;
			start = direction ? startPosition : endPosition;
			end = (direction ? endPosition : startPosition) + step;

			for (int i = start; i != end; i += step) {
				if (edit_positions [i].Editable && !edit_positions [i].FilledIn)
					return i;
			}
			return InvalidIndex;
		}

		public static bool GetOperationResultFromHint (MaskedTextResultHint hint)
		{
			return (hint == MaskedTextResultHint.CharacterEscaped || 
				hint == MaskedTextResultHint.NoEffect || 
				hint == MaskedTextResultHint.SideEffect || 
				hint == MaskedTextResultHint.Success);
		}

		public bool InsertAt (char input, int position)
		{
			int testPosition;
			MaskedTextResultHint resultHint;
			return InsertAt (input, position, out testPosition, out resultHint);
		}

		public bool InsertAt (string input, int position)
		{
			int testPosition;
			MaskedTextResultHint resultHint;
			return InsertAt (input, position, out testPosition, out resultHint);
		}

		public bool InsertAt (char input, int position, out int testPosition, out MaskedTextResultHint resultHint)
		{
			return InsertAt (input.ToString (), position, out testPosition, out resultHint);		
		}

		public bool InsertAt (string input, int position, out int testPosition, out MaskedTextResultHint resultHint)
		{
			if (input == null)
				throw new ArgumentNullException ("input");
			
			if (position >= edit_positions.Length) {
				testPosition = position;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}
			
			if (input == string.Empty) {
				testPosition = position;
				resultHint = MaskedTextResultHint.NoEffect;
				return true;
			}
			
			bool result;

			result = InsertAtInternal (input, position, out testPosition, out resultHint, true);

			if (result) {	
				result = InsertAtInternal (input, position, out testPosition, out resultHint, false);
			}

			return result;	
		}

		public bool IsAvailablePosition (int position)
		{
			if (position < 0 || position >= edit_positions.Length)
				return false;
				
			return edit_positions [position].Editable && !edit_positions [position].FilledIn;
		}

		public bool IsEditPosition (int position)
		{
			if (position < 0 || position >= edit_positions.Length)
				return false;
				
			return edit_positions [position].Editable;
		}

		public static bool IsValidInputChar (char c)
		{
			return char.IsLetterOrDigit (c) || char.IsPunctuation (c) || char.IsSymbol (c) || c == ' ';
		}

		public static bool IsValidMaskChar (char c)
		{
			return char.IsLetterOrDigit (c) || char.IsPunctuation (c) || char.IsSymbol (c) || c == ' ';
		}

		public static bool IsValidPasswordChar (char c)
		{
			return char.IsLetterOrDigit (c) || char.IsPunctuation (c) || char.IsSymbol (c) || c == ' ' || c == char.MinValue;
		}

		public bool Remove ()
		{
			int testPosition;
			MaskedTextResultHint resultHint;
			return Remove (out testPosition, out resultHint);
		}

		public bool Remove (out int testPosition, out MaskedTextResultHint resultHint)
		{
			if (LastAssignedPosition == InvalidIndex) {
				resultHint = MaskedTextResultHint.NoEffect;
				testPosition = 0;
				return true;
			}
			
			testPosition = LastAssignedPosition;
			resultHint = MaskedTextResultHint.Success;
			edit_positions [LastAssignedPosition].input = char.MinValue;
			
			return true;
		}

		public bool RemoveAt (int position)
		{
			return RemoveAt (position, position);
		}

		public bool RemoveAt (int startPosition, int endPosition)
		{
			int testPosition;
			MaskedTextResultHint resultHint;
			return RemoveAt (startPosition, endPosition, out testPosition, out resultHint);
		}

		public bool RemoveAt (int startPosition, int endPosition, out int testPosition, out MaskedTextResultHint resultHint)
		{	
			bool result;
			
			result = RemoveAtInternal (startPosition, endPosition, out testPosition, out resultHint, true);
			
			if (result) {
				result = RemoveAtInternal (startPosition, endPosition, out testPosition, out resultHint, false);
			}
		
			return result;
		}

		public bool Replace (char input, int position)
		{
			int testPosition;
			MaskedTextResultHint resultHint;
			return Replace (input, position, out testPosition, out resultHint);
		}

		public bool Replace (string input, int position)
		{
			int testPosition;
			MaskedTextResultHint resultHint;
			return Replace (input, position, out testPosition, out resultHint);
		}

		public bool Replace (char input, int position, out int testPosition, out MaskedTextResultHint resultHint)
		{
			if (position < 0 || position >= edit_positions.Length) {
				testPosition = position;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}

			if (VerifyEscapeChar (input, position)) {
				if (edit_positions [position].FilledIn && edit_positions [position].Editable && (input == ' ' && ResetOnSpace) || (input == PromptChar && ResetOnPrompt)) {
					edit_positions [position].Reset ();
					resultHint = MaskedTextResultHint.SideEffect;
				} else {
					resultHint = MaskedTextResultHint.CharacterEscaped;
				}
				testPosition = position;
				return true;
			}
			
			int current_edit_position;
			current_edit_position = FindEditPositionFrom (position, true);

			if (current_edit_position == InvalidIndex) {
				testPosition = position;
				resultHint = MaskedTextResultHint.UnavailableEditPosition;
				return false;
			}

			if (!IsValidInputChar (input)) {
				testPosition = current_edit_position;
				resultHint = MaskedTextResultHint.InvalidInput;
				return false;
			}
			
			return ReplaceInternal (edit_positions, input, current_edit_position, out testPosition, out resultHint, false);
		}

		public bool Replace (string input, int position, out int testPosition, out MaskedTextResultHint resultHint)
		{
			if (input == null)
				throw new ArgumentNullException ("input");

			if (position < 0 || position >= edit_positions.Length) {
				testPosition = position;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}
			
			if (input.Length == 0) {
				return RemoveAt (position, position, out testPosition, out resultHint);
			}
			
			bool result;
			
			result = ReplaceInternal (input, position, edit_positions.Length - 1, out testPosition, out resultHint, true, true);
			
			if (result) {
				result = ReplaceInternal (input, position, edit_positions.Length - 1, out testPosition, out resultHint, false, true);	
			}

			return result;
		}

		public bool Replace (char input, int startPosition, int endPosition, out int testPosition, out MaskedTextResultHint resultHint)
		{

			if (endPosition >= edit_positions.Length) {
				testPosition = endPosition;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}

			if (startPosition < 0) {
				testPosition = startPosition;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}
			
			if (startPosition > endPosition) {
				testPosition = startPosition;
				resultHint = MaskedTextResultHint.PositionOutOfRange;
				return false;
			}
			
			if (startPosition == endPosition) {
				return ReplaceInternal (edit_positions, input, startPosition, out testPosition, out resultHint, false);
			}

			return Replace (input.ToString (), startPosition, endPosition, out testPosition, out resultHint);
		}

		public bool Replace (string input, int startPosition, int endPosition, out int testPosition, out MaskedTextResultHint resultHint)
		{
			bool result;
			
			result = ReplaceInternal (input, startPosition, endPosition, out testPosition, out resultHint, true, false);
			
			if (result) {
				result = ReplaceInternal (input, startPosition, endPosition, out testPosition, out resultHint, false, false);
			}
			return result;
		}

		public bool Set (string input)
		{
			int testPosition;
			MaskedTextResultHint resultHint;
			return Set (input, out testPosition, out resultHint);
		}

		public bool Set (string input, out int testPosition, out MaskedTextResultHint resultHint)
		{
			bool result;

			if (input == null) {
				throw new ArgumentNullException ("input");
			}

			result = VerifyStringInternal (input, out testPosition, out resultHint, 0, true);
			
			if (result) {
				result = VerifyStringInternal (input, out testPosition, out resultHint, 0, false);
			}
			
			return result;
		}

		public string ToDisplayString ()
		{
			return ToString (false, true, true, 0, Length);
		}

		public override string ToString ()
		{
			return ToString (true, IncludePrompt, IncludeLiterals, 0, Length);
		}

		public string ToString (bool ignorePasswordChar)
		{
			return ToString (ignorePasswordChar, IncludePrompt, IncludeLiterals, 0, Length);
		}

		public string ToString (bool includePrompt, bool includeLiterals)
		{
			return ToString (true, includePrompt, includeLiterals, 0, Length);
		}

		public string ToString (int startPosition, int length)
		{
			return ToString (true, IncludePrompt, IncludeLiterals, startPosition, length);
		}

		public string ToString (bool ignorePasswordChar, int startPosition, int length)
		{
			return ToString (ignorePasswordChar, IncludePrompt, IncludeLiterals, startPosition, length);
		}

		public string ToString (bool includePrompt, bool includeLiterals, int startPosition, int length)
		{
			return ToString (true, includePrompt, includeLiterals, startPosition, length);
		}

		public string ToString (bool ignorePasswordChar, bool includePrompt, bool includeLiterals, int startPosition, int length)
		{
			if (startPosition < 0)
				startPosition = 0;
			
			if (length <= 0)
				return string.Empty;
			
			StringBuilder result = new StringBuilder ();
			int start = startPosition;
			int end = startPosition + length - 1;
			
			if (end >= edit_positions.Length) {
				end = edit_positions.Length - 1;
			}
			int last_assigned_position = FindAssignedEditPositionInRange (start, end, false);
			
			// Find the last position in the mask to check for
			if (!includePrompt) {
				int last_non_edit_position;
				
				last_non_edit_position = FindNonEditPositionInRange (start, end, false);
				
				if (includeLiterals) {
					end = last_assigned_position > last_non_edit_position ? last_assigned_position : last_non_edit_position;
				} else {
					end = last_assigned_position;
				}
			}
			
			for (int i = start; i <= end; i++) {
				EditPosition ed = edit_positions [i];
				
				if (ed.Type == EditType.Literal) {
					if (includeLiterals) {
						result.Append (ed.Text);
					}				
				} else if (ed.Editable) {
					if (IsPassword) {
						if (!ed.FilledIn) { // Nothing to hide or show.
							if (includePrompt)
								result.Append (PromptChar);
							else
								result.Append (" ");
						} else if (ignorePasswordChar)
							result.Append (ed.Input);
						else
							result.Append (PasswordChar);
					} else if (!ed.FilledIn) {
						if (includePrompt) {
							result.Append (PromptChar);
						} else if (includeLiterals) {
							result.Append (" ");
						} else if (last_assigned_position != InvalidIndex && last_assigned_position > i) {
							result.Append (" ");
						}
					} else {
						result.Append (ed.Text);
					}
				} else {
					if (includeLiterals)
						result.Append (ed.Text);
				}
			}
			
			return result.ToString ();
		}

		public bool VerifyChar (char input, int position, out MaskedTextResultHint hint)
		{
			return VerifyCharInternal (input, position, out hint, true);
		}

		public bool VerifyEscapeChar (char input, int position)
		{
			if (position >= edit_positions.Length || position < 0) {
				return false;
			}
			
			if (!edit_positions [position].Editable) {
				if (SkipLiterals) {
					return input.ToString () == edit_positions [position].Text;
				} else {
					return false;
				}
			}
			
			if (ResetOnSpace && input == ' ') {
				return true;
			} else if (ResetOnPrompt && input == PromptChar) {
				return true;
			} else {
				return false;
			}
		}

		public bool VerifyString (string input)
		{
			int testPosition;
			MaskedTextResultHint resultHint;
			return VerifyString (input, out testPosition, out resultHint);
		}

		public bool VerifyString (string input, out int testPosition, out MaskedTextResultHint resultHint)
		{
			if (input == null || input.Length == 0) {			
				testPosition = 0;
				resultHint = MaskedTextResultHint.NoEffect;
				return true;
			}
			
			return VerifyStringInternal (input, out testPosition, out resultHint, 0, true);
		}
#endregion

#region Public properties
		public bool AllowPromptAsInput {
			get {
				return allow_prompt_as_input;
			}
		}
		
		public bool AsciiOnly {
			get {
				return ascii_only;
			}
		}
		
		public int AssignedEditPositionCount {
			get {
				int result = 0;
				for (int i = 0; i < edit_positions.Length; i++) {
					if (edit_positions [i].FilledIn) {
						result++;
					}
				}
				return result;
			}
		}
		
		public int AvailableEditPositionCount {
			get {
				int result = 0;
				foreach (EditPosition edit in edit_positions) {
					if (!edit.FilledIn && edit.Editable) {
						result++;
					}
				}
				return result;
			}
		}
		
		public CultureInfo Culture {
			get {
				return culture;
			}
		}
		
		public static char DefaultPasswordChar {
			get {
				return '*';
			}
		}
		
		public int EditPositionCount {
			get {
				int result = 0;
				foreach (EditPosition edit in edit_positions) {
					if (edit.Editable) {
						result++;
					}
				}
						
				return result;
			}
		}
		
		public IEnumerator EditPositions {
			get {
				System.Collections.Generic.List <int> result = new System.Collections.Generic.List<int> ();
				for (int i = 0; i < edit_positions.Length; i++) {
					if (edit_positions [i].Editable) {
						result.Add (i);
					}
				}
				return result.GetEnumerator ();
			}
		}
		
		public bool IncludeLiterals {
			get {
				return include_literals;
			}
			set {
				include_literals = value;
			}
		}
		
		public bool IncludePrompt {
			get {
				return include_prompt;
			}
			set {
				include_prompt = value;
			}
		}
		
		public static int InvalidIndex {
			get {
				return -1;
			}
		}
		
		public bool IsPassword {
			get {
				return password_char != char.MinValue;
			}
			set {
				password_char = value ? DefaultPasswordChar : char.MinValue;
			}
		}
		
		public char this [int index] {
			get {
				if (index < 0 || index >= Length) {
					throw new IndexOutOfRangeException (index.ToString ());
				}
				
				return ToString (true, true, true, 0, edit_positions.Length) [index];
			}
		}
		
		public int LastAssignedPosition {
			get {
				return FindAssignedEditPositionFrom (edit_positions.Length - 1, false);
			}
		}
		
		public int Length {
			get {
				int result = 0;
				for (int i = 0; i < edit_positions.Length; i++) {
					if (edit_positions [i].Visible)
						result++;
				}
				return result;
			}
		}
		
		public string Mask {
			get {
				return mask;
			}
		}
		
		public bool MaskCompleted {
			get {
				for (int i = 0; i < edit_positions.Length; i++)
					if (edit_positions [i].Required && !edit_positions [i].FilledIn)
						return false;
				return true;
			}
		}
		
		public bool MaskFull {
			get {
				for (int i = 0; i < edit_positions.Length; i++)
					if (edit_positions [i].Editable && !edit_positions [i].FilledIn)
						return false;
				return true;
			}
		}
		
		public char PasswordChar {
			get {
				return password_char;
			}
			set {
				password_char = value;
			}
		}
		
		public char PromptChar {
			get {
				return prompt_char;
			}
			set {
				prompt_char = value;
			}
		}
		
		public bool ResetOnPrompt {
			get {
				return reset_on_prompt;
			}
			set {
				reset_on_prompt = value;
			}
		}
		
		public bool ResetOnSpace {
			get {
				return reset_on_space;
			}
			set {
				reset_on_space = value;
			}
		}
		
		public bool SkipLiterals {
			get {
				return skip_literals;
			}
			set {
				skip_literals = value;
			}
		}
#endregion

	}
}