Skip to content

Commit 35958dd

Browse files
author
Морозов Денис Сергеевич
committed
Merge pull request #21 in UPCMC/ftctextentryformatting from improve-calculating-of-count-mask-characters to master
* commit '31f721e0ae816b798f9bdf0ef6d77450aceb1676': Fix CR fix unit tests Improve caclucating of count mask characters
2 parents ff6aacd + 31f721e commit 35958dd

9 files changed

Lines changed: 59 additions & 44 deletions

File tree

FTCTextEntryFormatting.xcodeproj/project.pbxproj

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
/* Begin PBXBuildFile section */
1010
21591A1641163D3E4D1A1F34 /* FTCDigitsInputFilterTestCaseWithZeroMaxLength.m in Sources */ = {isa = PBXBuildFile; fileRef = F515E4501F5D58CD00AEEC8C /* FTCDigitsInputFilterTestCaseWithZeroMaxLength.m */; };
11+
B10523501F84C4B6002B3F61 /* FTCDigitsInputFilterTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = B105234E1F84C497002B3F61 /* FTCDigitsInputFilterTestCase.m */; };
12+
B10523531F84C655002B3F61 /* FTCTextEntryFormattingConfigFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = B10523511F84C655002B3F61 /* FTCTextEntryFormattingConfigFactory.h */; };
13+
B10523541F84C655002B3F61 /* FTCTextEntryFormattingConfigFactory.m in Sources */ = {isa = PBXBuildFile; fileRef = B10523521F84C655002B3F61 /* FTCTextEntryFormattingConfigFactory.m */; };
1114
B1EBF8E71F2315F90014F4CB /* FTCTextEntryFormatting.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B1EBF8DD1F2315F90014F4CB /* FTCTextEntryFormatting.framework */; };
1215
B1EBF9441F2316410014F4CB /* FTCNoFormattingFormatter.h in Headers */ = {isa = PBXBuildFile; fileRef = B1EBF9001F2316410014F4CB /* FTCNoFormattingFormatter.h */; settings = {ATTRIBUTES = (Public, ); }; };
1316
B1EBF9451F2316410014F4CB /* FTCNoFormattingFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = B1EBF9011F2316410014F4CB /* FTCNoFormattingFormatter.m */; };
@@ -66,7 +69,6 @@
6669
B1EBF9DF1F231C190014F4CB /* FTCPostfixFormatterTestCaseWithNotEmptyPostfix.m in Sources */ = {isa = PBXBuildFile; fileRef = B1EBF9D71F231C190014F4CB /* FTCPostfixFormatterTestCaseWithNotEmptyPostfix.m */; };
6770
CCC1C0B14A2C0AF523465DB5 /* FTCMaskFormatterGenericConfig.h in Headers */ = {isa = PBXBuildFile; fileRef = CCC1C3A298D5610F9EAECBC1 /* FTCMaskFormatterGenericConfig.h */; settings = {ATTRIBUTES = (Public, ); }; };
6871
CCC1CD063EECBF9CCDDE12F9 /* FTCMaskFormatterGenericConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = CCC1CD7598485396BE2F9CEF /* FTCMaskFormatterGenericConfig.m */; };
69-
F5859D5F1F5E4E1D00607B61 /* FTCDigitsInputFilterTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = F5859D5E1F5E4E1D00607B61 /* FTCDigitsInputFilterTestCase.m */; };
7072
/* End PBXBuildFile section */
7173

7274
/* Begin PBXContainerItemProxy section */
@@ -80,6 +82,9 @@
8082
/* End PBXContainerItemProxy section */
8183

8284
/* Begin PBXFileReference section */
85+
B105234E1F84C497002B3F61 /* FTCDigitsInputFilterTestCase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FTCDigitsInputFilterTestCase.m; path = InputFilter/FTCDigitsInputFilterTestCase.m; sourceTree = "<group>"; };
86+
B10523511F84C655002B3F61 /* FTCTextEntryFormattingConfigFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FTCTextEntryFormattingConfigFactory.h; sourceTree = "<group>"; };
87+
B10523521F84C655002B3F61 /* FTCTextEntryFormattingConfigFactory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FTCTextEntryFormattingConfigFactory.m; sourceTree = "<group>"; };
8388
B1370D1E1F4E849800E8EBA3 /* FTCTextEntryFormatting-prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "FTCTextEntryFormatting-prefix.pch"; sourceTree = "<group>"; };
8489
B1EBF8DD1F2315F90014F4CB /* FTCTextEntryFormatting.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FTCTextEntryFormatting.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8590
B1EBF8E61F2315F90014F4CB /* FTCTextEntryFormattingTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FTCTextEntryFormattingTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -144,7 +149,6 @@
144149
CCC1C3A298D5610F9EAECBC1 /* FTCMaskFormatterGenericConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FTCMaskFormatterGenericConfig.h; sourceTree = "<group>"; };
145150
CCC1CD7598485396BE2F9CEF /* FTCMaskFormatterGenericConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FTCMaskFormatterGenericConfig.m; sourceTree = "<group>"; };
146151
F515E4501F5D58CD00AEEC8C /* FTCDigitsInputFilterTestCaseWithZeroMaxLength.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FTCDigitsInputFilterTestCaseWithZeroMaxLength.m; path = InputFilter/FTCDigitsInputFilterTestCaseWithZeroMaxLength.m; sourceTree = "<group>"; };
147-
F5859D5E1F5E4E1D00607B61 /* FTCDigitsInputFilterTestCase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FTCDigitsInputFilterTestCase.m; path = FormatCoordinator/FTCDigitsInputFilterTestCase.m; sourceTree = "<group>"; };
148152
/* End PBXFileReference section */
149153

150154
/* Begin PBXFrameworksBuildPhase section */
@@ -201,6 +205,8 @@
201205
B1EBF8FB1F2316410014F4CB /* Src */ = {
202206
isa = PBXGroup;
203207
children = (
208+
B10523511F84C655002B3F61 /* FTCTextEntryFormattingConfigFactory.h */,
209+
B10523521F84C655002B3F61 /* FTCTextEntryFormattingConfigFactory.m */,
204210
B1EBF8FE1F2316410014F4CB /* Core */,
205211
B1EBF92D1F2316410014F4CB /* Money */,
206212
B1EBF93C1F2316410014F4CB /* Tools */,
@@ -375,7 +381,7 @@
375381
F515E4521F5D58D200AEEC8C /* InputFilter */ = {
376382
isa = PBXGroup;
377383
children = (
378-
F5859D5E1F5E4E1D00607B61 /* FTCDigitsInputFilterTestCase.m */,
384+
B105234E1F84C497002B3F61 /* FTCDigitsInputFilterTestCase.m */,
379385
F515E4501F5D58CD00AEEC8C /* FTCDigitsInputFilterTestCaseWithZeroMaxLength.m */,
380386
);
381387
name = InputFilter;
@@ -390,6 +396,7 @@
390396
files = (
391397
B1EBF9751F2316410014F4CB /* FTCMoneyEntryNotEditingInputFilter.h in Headers */,
392398
B1EBF9731F2316410014F4CB /* FTCMoneyEntryNotEditingFormatter.h in Headers */,
399+
B10523531F84C655002B3F61 /* FTCTextEntryFormattingConfigFactory.h in Headers */,
393400
B1EBF9551F2316410014F4CB /* FTCFilteredString.h in Headers */,
394401
B1EBF9711F2316410014F4CB /* FTCMoneyEntryFormatUtils.h in Headers */,
395402
B1EBF94C1F2316410014F4CB /* FTCTextEntry.h in Headers */,
@@ -523,6 +530,7 @@
523530
B1EBF9561F2316410014F4CB /* FTCFilteredString.m in Sources */,
524531
B1EBF95C1F2316410014F4CB /* FTCLimitedLengthInputFilter.m in Sources */,
525532
B1EBF9501F2316410014F4CB /* FTCTextEntryFormatCoordinatorHelper.m in Sources */,
533+
B10523541F84C655002B3F61 /* FTCTextEntryFormattingConfigFactory.m in Sources */,
526534
B1EBF9451F2316410014F4CB /* FTCNoFormattingFormatter.m in Sources */,
527535
B1EBF96C1F2316410014F4CB /* FTCIntegralMoneyAmountEntryNotEditingInputFilter.m in Sources */,
528536
B1EBF9481F2316410014F4CB /* FTCMaskFormatter.m in Sources */,
@@ -549,7 +557,7 @@
549557
buildActionMask = 2147483647;
550558
files = (
551559
B1EBF9DA1F231C190014F4CB /* FTCMaskFormatterTestCase.m in Sources */,
552-
F5859D5F1F5E4E1D00607B61 /* FTCDigitsInputFilterTestCase.m in Sources */,
560+
B10523501F84C4B6002B3F61 /* FTCDigitsInputFilterTestCase.m in Sources */,
553561
B1EBF9DE1F231C190014F4CB /* FTCPostfixFormatterTestCaseWithNilPostfix.m in Sources */,
554562
B1EBF9DF1F231C190014F4CB /* FTCPostfixFormatterTestCaseWithNotEmptyPostfix.m in Sources */,
555563
B1EBF9DD1F231C190014F4CB /* FTCPostfixFormatterTestCaseWithEmptyPostfix.m in Sources */,

Src/Core/Formatting/MaskFormatter/FTCMaskFormatter.m

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,20 @@ - (NSString *)rawFromFormatted:(NSString *)formattedValue
4646

4747
- (NSString *)formattedFromRaw:(NSString *)rawValue
4848
{
49-
NSString * const format = config.format;
49+
NSString * const mask = config.mask;
5050

51-
NSMutableString *formattedValue = [[NSMutableString alloc] initWithCapacity:format.length];
51+
NSMutableString *formattedValue = [[NSMutableString alloc] initWithCapacity:mask.length];
5252

53-
for( NSUInteger indexInFormat = 0, indexInRawValue = 0; indexInFormat < format.length; ++indexInFormat )
53+
for( NSUInteger indexInFormat = 0, indexInRawValue = 0; indexInFormat < mask.length; ++indexInFormat )
5454
{
5555
if( config.cutTail && indexInRawValue == rawValue.length )
5656
{
5757
break;
5858
}
5959

60-
if( config.maskCharacter != [format characterAtIndex:indexInFormat] || indexInRawValue >= rawValue.length )
60+
if( config.maskCharacter != [mask characterAtIndex:indexInFormat] || indexInRawValue >= rawValue.length )
6161
{
62-
unichar c = [format characterAtIndex:indexInFormat];
62+
unichar c = [mask characterAtIndex:indexInFormat];
6363
[formattedValue appendString:[FTCTextEntryFormattingStringUtils stringWithCharacter:c]];
6464
}
6565
else
@@ -122,9 +122,9 @@ - (NSUInteger)positionOfRawSymbolInFormattedValue:(const NSUInteger)rawNumber
122122

123123
NSUInteger currentPosition = 0;
124124

125-
for( NSUInteger i = 0; i < config.format.length; ++i )
125+
for( NSUInteger i = 0; i < config.mask.length; ++i )
126126
{
127-
if( config.maskCharacter != [config.format characterAtIndex:i] )
127+
if( config.maskCharacter != [config.mask characterAtIndex:i] )
128128
{
129129
continue;
130130
}
@@ -145,13 +145,13 @@ - (NSUInteger)positionOfRawSymbolInFormattedValue:(const NSUInteger)rawNumber
145145
- (nonnull NSString *)rawValueStringFromFormattedString:(nullable NSString *const)formattedString
146146
inFormattedRange:(const NSRange)range
147147
{
148-
NSString * const format = config.format;
148+
NSString * const mask = config.mask;
149149

150150
NSMutableString *rawString = [[NSMutableString alloc] init];
151151

152152
for( NSUInteger i = range.location; (i < range.location + range.length) && (i < formattedString.length); ++i )
153153
{
154-
if ( config.maskCharacter == [format characterAtIndex:i] && config.maskCharacter != [formattedString characterAtIndex:i] )
154+
if ( config.maskCharacter == [mask characterAtIndex:i] && config.maskCharacter != [formattedString characterAtIndex:i] )
155155
{
156156
unichar c = [formattedString characterAtIndex:i];
157157
[rawString appendString:[FTCTextEntryFormattingStringUtils stringWithCharacter:c]];

Src/Core/Formatting/MaskFormatter/FTCMaskFormatterConfig.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ NS_ASSUME_NONNULL_BEGIN
2222

2323
@protocol FTCMaskFormatterConfig<NSObject>
2424

25-
@property (nonatomic, readonly) NSString *format;
25+
@property (nonatomic, readonly) NSString *mask;
2626
@property (nonatomic, readonly) unichar maskCharacter;
27+
@property (nonatomic, readonly) NSUInteger countMaskCharacters;
2728
@property (nonatomic, readonly) BOOL cutTail;
2829

2930
@end

Src/Core/Formatting/MaskFormatter/FTCMaskFormatterGenericConfig.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ NS_ASSUME_NONNULL_BEGIN
2424

2525
@interface FTCMaskFormatterGenericConfig : NSObject<FTCMaskFormatterConfig>
2626

27-
@property (nonatomic, readwrite) unichar maskCharacter; // default '_'
2827
@property (nonatomic, readwrite) BOOL cutTail; // default NO
2928

30-
- (instancetype)initWithFormat:(NSString *)format NS_DESIGNATED_INITIALIZER;
29+
- (instancetype)initWithMask:(NSString *)mask maskCharacter:(NSString *)maskCharacter NS_DESIGNATED_INITIALIZER;
3130

3231
- (instancetype)init NS_UNAVAILABLE;
3332

Src/Core/Formatting/MaskFormatter/FTCMaskFormatterGenericConfig.m

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,39 @@
2222

2323
@implementation FTCMaskFormatterGenericConfig
2424

25-
@synthesize format = _format;
25+
@synthesize mask = _mask;
26+
@synthesize maskCharacter = _maskCharacter;
27+
@synthesize countMaskCharacters = _countMaskCharacters;
2628

27-
- (instancetype)init
29+
- (instancetype)initWithMask:(NSString *)mask maskCharacter:(NSString *)maskCharacter
2830
{
29-
assert( false && @"Won't happen" );
30-
return nil;
31-
}
32-
33-
- (instancetype)initWithFormat:(NSString *)format
34-
{
35-
assert( nil != format );
31+
assert( nil != mask );
32+
assert( maskCharacter.length == 1 );
3633

3734
self = [super init];
3835

39-
_format = format;
40-
_maskCharacter = '_';
36+
_mask = mask;
37+
_maskCharacter = [maskCharacter characterAtIndex:0];
4138
_cutTail = NO;
4239

40+
NSUInteger countMaskCharacters = 0;
41+
for( NSUInteger i = 0; i < _mask.length; ++i )
42+
{
43+
if( [_mask characterAtIndex:i] == _maskCharacter )
44+
{
45+
++countMaskCharacters;
46+
}
47+
}
48+
49+
_countMaskCharacters = countMaskCharacters;
50+
4351
return self;
4452
}
4553

54+
- (instancetype)init
55+
{
56+
assert( false && @"Won't happen" );
57+
return nil;
58+
}
59+
4660
@end

Src/Core/InputFiltering/LimitedLengthInputFilter/FTCLimitedLengthInputFilter.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
// SOFTWARE.
2020

21-
@import Foundation;
22-
2321
#import "FTCTextEntryEditingInputFilter.h"
2422
#import "FTCTextEntryNotEditingInputFilter.h"
2523

Src/FTCTextEntryFormattingConfigFactory.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ NS_ASSUME_NONNULL_BEGIN
99

1010
@interface FTCTextEntryFormattingConfigFactory : NSObject
1111

12-
+ (FTCTextEntryFormattingConfig *)mobilePhoneConfigWithFormat:(NSString *)format
13-
maskChar:(unichar)maskChar
14-
maskCharsCount:(NSUInteger)maskCharsCount;
12+
+ (FTCTextEntryFormattingConfig *)mobilePhoneConfigWithMask:(NSString *)mask
13+
maskChar:(NSString *)maskChar NS_SWIFT_NAME( mobilePhoneConfig(with:maskChar:) );
1514

16-
+ (FTCTextEntryFormattingConfig *)moneyConfigWithCurrency:(nullable NSString *)currency onlyIntegral:(BOOL)onlyIntegral;
15+
+ (FTCTextEntryFormattingConfig *)moneyConfigWithCurrency:(nullable NSString *)currency
16+
onlyIntegral:(BOOL)onlyIntegral NS_SWIFT_NAME( moneyConfig(with:onlyIntegral:) );
1717

1818
- (instancetype)init NS_UNAVAILABLE;
1919

Src/FTCTextEntryFormattingConfigFactory.m

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@
1717

1818
@implementation FTCTextEntryFormattingConfigFactory
1919

20-
+ (FTCTextEntryFormattingConfig *)mobilePhoneConfigWithFormat:(NSString *)format
21-
maskChar:(unichar)maskChar
22-
maskCharsCount:(NSUInteger)maskCharsCount
20+
+ (FTCTextEntryFormattingConfig *)mobilePhoneConfigWithMask:(NSString *)mask maskChar:(NSString *)maskChar
2321
{
24-
__auto_type maskConfig = [[FTCMaskFormatterGenericConfig alloc] initWithFormat:format];
25-
maskConfig.maskCharacter = maskChar;
22+
__auto_type maskConfig = [[FTCMaskFormatterGenericConfig alloc] initWithMask:mask maskCharacter:maskChar];
2623

2724
__auto_type maskFormatter = [[FTCMaskFormatter alloc] initWithConfig:maskConfig];
28-
__auto_type inputFilter = [[FTCDigitsValueFilter alloc] initWithMaxLength:maskCharsCount];
25+
__auto_type inputFilter = [[FTCDigitsValueFilter alloc] initWithMaxLength:maskConfig.countMaskCharacters];
2926

3027
__auto_type formattingConfig = [[FTCTextEntryFormattingConfig alloc] init];
3128

@@ -64,4 +61,4 @@ - (instancetype)init
6461
return nil;
6562
}
6663

67-
@end
64+
@end

UnitTests/Formatters/FTCMaskFormatter/FTCMaskFormatterTestCase.m

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@ - (void)setUp
3535
{
3636
[super setUp];
3737

38-
FTCMaskFormatterGenericConfig *configWithCutTail = [[FTCMaskFormatterGenericConfig alloc] initWithFormat:@"_ _ _ _ _"];
39-
configWithCutTail.maskCharacter = '_';
38+
FTCMaskFormatterGenericConfig *configWithCutTail = [[FTCMaskFormatterGenericConfig alloc] initWithMask:@"_ _ _ _ _" maskCharacter:@"_"];
4039
configWithCutTail.cutTail = YES;
4140

4241
formatterWithCutTail = [[FTCMaskFormatter alloc] initWithConfig:configWithCutTail];
4342

44-
FTCMaskFormatterGenericConfig *configWithoutCutTail = [[FTCMaskFormatterGenericConfig alloc] initWithFormat:@"_ _ _ _ _"];
45-
configWithoutCutTail.maskCharacter = '_';
43+
FTCMaskFormatterGenericConfig *configWithoutCutTail = [[FTCMaskFormatterGenericConfig alloc] initWithMask:@"_ _ _ _ _" maskCharacter:@"_"];
4644
configWithoutCutTail.cutTail = NO;
4745

4846
formatterWithoutCutTail = [[FTCMaskFormatter alloc] initWithConfig:configWithoutCutTail];

0 commit comments

Comments
 (0)