Skip to content

Commit

Permalink
fix bugs. position of matched equal sign should be first.
Browse files Browse the repository at this point in the history
also support comments.
  • Loading branch information
qfish committed Dec 4, 2013
1 parent 1701c83 commit 9420d2b
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 90 deletions.
12 changes: 6 additions & 6 deletions main/main.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
E9E24B181837D9960040C1B4 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = E9E24B171837D9960040C1B4 /* AppDelegate.m */; };
E9E24B1B1837D9960040C1B4 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = E9E24B191837D9960040C1B4 /* MainMenu.xib */; };
E9E24B1D1837D9960040C1B4 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E9E24B1C1837D9960040C1B4 /* Images.xcassets */; };
E9E24B391837D9CA0040C1B4 /* 1.txt in Resources */ = {isa = PBXBuildFile; fileRef = E9E24B381837D9CA0040C1B4 /* 1.txt */; };
E9E24B391837D9CA0040C1B4 /* equalSign.txt in Resources */ = {isa = PBXBuildFile; fileRef = E9E24B381837D9CA0040C1B4 /* equalSign.txt */; };
E9EF7F111847A09A00F2C5D5 /* 2.txt in Resources */ = {isa = PBXBuildFile; fileRef = E9EF7F0F1847A09A00F2C5D5 /* 2.txt */; };
E9EF7F121847A09A00F2C5D5 /* 3.txt in Resources */ = {isa = PBXBuildFile; fileRef = E9EF7F101847A09A00F2C5D5 /* 3.txt */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -49,7 +49,7 @@
E9E24B1A1837D9960040C1B4 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = "<group>"; };
E9E24B1C1837D9960040C1B4 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
E9E24B231837D9960040C1B4 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
E9E24B381837D9CA0040C1B4 /* 1.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = 1.txt; sourceTree = "<group>"; };
E9E24B381837D9CA0040C1B4 /* equalSign.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = equalSign.txt; sourceTree = "<group>"; };
E9EF7F0F1847A09A00F2C5D5 /* 2.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = 2.txt; sourceTree = "<group>"; };
E9EF7F101847A09A00F2C5D5 /* 3.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = 3.txt; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -124,7 +124,7 @@
E9E24B161837D9960040C1B4 /* AppDelegate.h */,
E9E24B171837D9960040C1B4 /* AppDelegate.m */,
E945BCA0184A44AE000E90FA /* patterns.plist */,
E9E24B381837D9CA0040C1B4 /* 1.txt */,
E9E24B381837D9CA0040C1B4 /* equalSign.txt */,
E9EF7F0F1847A09A00F2C5D5 /* 2.txt */,
E9EF7F101847A09A00F2C5D5 /* 3.txt */,
E9C45A4A18499A7800ACB37B /* 4.txt */,
Expand Down Expand Up @@ -200,7 +200,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E9E24B391837D9CA0040C1B4 /* 1.txt in Resources */,
E9E24B391837D9CA0040C1B4 /* equalSign.txt in Resources */,
E9E24B0F1837D9960040C1B4 /* InfoPlist.strings in Resources */,
E9E24B1D1837D9960040C1B4 /* Images.xcassets in Resources */,
E9E24B151837D9960040C1B4 /* Credits.rtf in Resources */,
Expand Down Expand Up @@ -336,7 +336,7 @@
"$(SYSTEM_APPS_DIR)/Xcode.app/Contents/SharedFrameworks",
"$(SYSTEM_APPS_DIR)/Xcode.app/Contents/Frameworks",
);
GCC_OPTIMIZATION_LEVEL = s;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "main/main-Prefix.pch";
INFOPLIST_FILE = "main/main-Info.plist";
Expand All @@ -357,7 +357,7 @@
"$(SYSTEM_APPS_DIR)/Xcode.app/Contents/SharedFrameworks",
"$(SYSTEM_APPS_DIR)/Xcode.app/Contents/Frameworks",
);
GCC_OPTIMIZATION_LEVEL = s;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "main/main-Prefix.pch";
INFOPLIST_FILE = "main/main-Info.plist";
Expand Down
24 changes: 0 additions & 24 deletions main/main/1.txt

This file was deleted.

24 changes: 12 additions & 12 deletions main/main/3.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
NSEvent * _currentEvent;
id _windowList;
id _keyWindow;
id _mainWindow;
id _delegate;
id *_hiddenList;
int _hiddenCount;
NSInteger _context;
void*_appleEventSuspensionID;
__weak id _previousKeyWindow;
short _unusedApp;
short _running;
NSEvent * _currentEvent;
id _windowList;
id _keyWindow;
id _mainWindow;
id _delegate;
id * _hiddenList;
int _hiddenCount;
NSInteger _context;
void * _appleEventSuspensionID;
__weak id _previousKeyWindow;
short _unusedApp;
short _running;
5 changes: 4 additions & 1 deletion main/main/4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
@property (strong, nonatomic) NSString * headURL; /*sad*/
@property (strong, nonatomic) NSString * dtype; //1qq 2新浪 3游客
@property (strong, nonatoasdmic) NSString * followNums;
@property (strong, nonatomic) NSString *fanNums;
@property (strong, nonatomic) NSString *fanNums;

@property (strong, nonatomic) UserModel * userInfo;
@property (weak, nonatomic) IBOutlet UIButton * skipButton;
11 changes: 5 additions & 6 deletions main/main/5.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
AS_MODEL( ActivityInfoModel, model1 );
AS_MODEL( FeedListModel, model2 );

AS_OUTLET( BeeUIScrollView, list );
AS_OUTLET( BeeUIButton, mask );
AS_OUTLET( TopMenu_iPhone, menu );
@"profile_image_url":self.userInfo.headURL,
@"gender":self.userInfo.gender,
@"objectid":self.userInfo.userId,
@"nick":self.userInfo.nick,
@"description":self.userInfo.signature
32 changes: 23 additions & 9 deletions main/main/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ @implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// [self test1];
[self testEqualSign];
// [self test2];
// [self test3];
// [self test4];
// [self test5];

NSArray * pgs = [XAlignPatternManager patternGroupsWithContentsOfFile:@"patterns"];
NSString * replace = [[self testFile:@"4.txt"] stringByAligningWithPatterns:pgs[2]];
NSString * replace = [[self testFile:@"equalSign.txt"] stringByAligningWithPatterns:pgs[0]];

NSLog( @"\n%@", replace );
}
Expand Down Expand Up @@ -50,7 +50,7 @@ - (NSString *)testFile:(NSString *)name
return string;
}

- (void)test1
- (void)testEqualSign
{
XAlignPattern * p1 = [[XAlignPattern alloc] init];
p1.string = @"^\\s*";
Expand All @@ -61,14 +61,29 @@ - (void)test1
};

XAlignPattern * p2 = [[XAlignPattern alloc] init];
p2.string = @"\\s*(?<=[\\w\\s])=(?=[\\w\\s])\\s*"; // just '=', exclude sth like '==' etc.
p2.position = XAlignPositionFisrt;
p2.string = @"\\s*(?<=[\\w\\s])[\\|\\+\\-\\*]?=(?=[\\w\\s])\\s*"; // just '=', exclude sth like '==' etc.
p2.control = ^ NSString * ( NSUInteger padding, NSString * match ) {
return @" = ";
};

NSArray * patterns = @[p1, p2];
XAlignPattern * p3 = [[XAlignPattern alloc] init];
p3.isOptional = YES;
p3.string = @"\\s*//.*$";
p3.control = ^ NSString * ( NSUInteger padding, NSString * match ) {
return [NSString stringWithFormat:@" %@", match.xtrim];
};

XAlignPattern * p4 = [[XAlignPattern alloc] init];
p4.isOptional = YES;
p4.string = @"\\s*/\\*.*$";
p4.control = ^ NSString * ( NSUInteger padding, NSString * match ) {
return [NSString stringWithFormat:@" %@", match.xtrim];
};

NSString * replace = [[self testFile:@"1.txt"] stringByAligningWithPatterns:patterns];
NSArray * patterns = @[p1, p2, p3, p4];

NSString * replace = [[self testFile:@"equalSign.txt"] stringByAligningWithPatterns:patterns];
NSLog( @"\n%@", replace );
}

Expand All @@ -92,15 +107,14 @@ - (void)test2
p3.isOptional = YES;
p3.string = @"\\s*//.*$";
p3.control = ^ NSString * ( NSUInteger padding, NSString * match ) {
// NSLog( @"%d =%@=", padding, match);
return [NSString stringWithFormat:@"+++|%@", match.xtrim];
return [NSString stringWithFormat:@" %@", match.xtrim];
};

XAlignPattern * p4 = [[XAlignPattern alloc] init];
p4.isOptional = YES;
p4.string = @"\\s*/\\*.*$";
p4.control = ^ NSString * ( NSUInteger padding, NSString * match ) {
return [NSString stringWithFormat:@"+++|%@", match.xtrim];
return [NSString stringWithFormat:@" %@", match.xtrim];
};

NSArray * patterns = @[p1, p2, p3, p4];
Expand Down
5 changes: 5 additions & 0 deletions main/main/equalSign.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
static const int kSampleRate = 44100; // asdasd
// asdasda = asdsd;
static const int kFormatID = kAudioFormatLinearPCM;
static const CGSize CollectionViewCellSize = {.height = 145, .width = 100};
static NSString *const CollectionViewCellIdentifier = @"MyListenedCell";
32 changes: 32 additions & 0 deletions main/main/patterns.plist
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
<dict>
<key>string</key>
<string>\s*(?&lt;=[\w\s])[\|\+\-\*]?=(?=[\w\s])\s*</string>
<key>position</key>
<integer>-1</integer>
<key>control</key>
<dict>
<key>needTrim</key>
Expand All @@ -43,6 +45,36 @@
<true/>
</dict>
</dict>
<dict>
<key>string</key>
<string>\s*//.*$</string>
<key>isOptional</key>
<true/>
<key>control</key>
<dict>
<key>format</key>
<string> %@</string>
<key>needFormat</key>
<true/>
<key>needTrim</key>
<true/>
</dict>
</dict>
<dict>
<key>string</key>
<string>\s*/\*.*$</string>
<key>isOptional</key>
<true/>
<key>control</key>
<dict>
<key>format</key>
<string> %@</string>
<key>needFormat</key>
<true/>
<key>needTrim</key>
<true/>
</dict>
</dict>
</array>
</dict>
<dict>
Expand Down
5 changes: 5 additions & 0 deletions xalign/NSString+XAlign.m
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ - (NSArray *)componentsSeparatedByRegexPattern:(NSString *)pattern position:(XAl

NSTextCheckingResult * matchResult = nil;

// for ( matchResult in matches )
// {
// NSLog( @"|||%@|||", [self substringWithRange:NSMakeRange(0, NSMaxRange([matchResult range]))]);
// }

switch ( position )
{
case XAlignPositionFisrt:
Expand Down
6 changes: 3 additions & 3 deletions xalign/XAlignPattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#import "blade.h"

typedef enum XAlignPosition{
XAlignPositionFisrt = 0,
XAlignPositionLast = NSIntegerMax,
XAlignPositionFisrt = -1,
XAlignPositionLast,
} XAlignPosition;

typedef enum XAlignPaddingMode {
Expand All @@ -30,7 +30,7 @@ typedef NSString * (^XAlignPatternControlBlockUS)(NSUInteger padding, NSString *
@interface XAlignPattern : NSObject
@property (nonatomic, assign) BOOL isOptional;
@property (nonatomic, retain) NSString * string;
@property (nonatomic, assign) NSUInteger position;
@property (nonatomic, assign) XAlignPosition position;
@property (nonatomic, assign) XAlignPaddingMode headMode;
@property (nonatomic, assign) XAlignPaddingMode matchMode;
@property (nonatomic, assign) XAlignPaddingMode tailMode;
Expand Down
52 changes: 23 additions & 29 deletions xalign/XAlignPattern.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define kPatterns @"patterns"
#define kPatternID @"id"
#define kPatternType @"type"
#define kPatternPosition @"position"
#define kPatternHeadMode @"headMode"
#define kPatternTailMode @"tailMode"
#define kPatternMatchMode @"matchMode"
Expand Down Expand Up @@ -48,15 +49,6 @@ + (NSString *)stringWithFormat:(NSString *)format

@implementation XAlignPattern

- (id)init
{
self = [super init];
if (self) {
self.position = XAlignPositionLast;
}
return self;
}

- (NSString *)description
{
return self.string;
Expand Down Expand Up @@ -135,18 +127,18 @@ + (NSArray *)patternGroupMatchWithString:(NSString *)string
+ (NSArray *)patternGroupsWithContentsOfFile:(NSString *)name
{
NSString * filePath = [[NSBundle mainBundle] pathForResource:name ofType:@"plist"];
NSArray * rawArray = [NSArray arrayWithContentsOfFile:filePath];
NSArray * rawArray = [[NSArray alloc]initWithContentsOfFile:filePath];
return [self patternGroupsWithRawArray:rawArray];
}

+ (NSArray *)patternGroupsWithRawArray:(NSArray *)array
+ (NSArray *)patternGroupsWithRawArray:(NSArray *)rawArray
{
if ( !array )
if ( !rawArray )
return nil;

NSMutableArray * patternGroups = [NSMutableArray array];

for ( NSDictionary * dict in array )
for ( NSDictionary * dict in rawArray )
{
NSArray * patternGroup = [self patternGroupWithDictinary:dict];

Expand Down Expand Up @@ -176,29 +168,31 @@ + (NSArray *)patternGroupWithDictinary:(NSDictionary *)dictionary
{
NSString * string = pattern[kPatternString];

BOOL isOptional = [pattern[kPatternIsOptional] intValue];
XAlignPaddingMode headMode = [pattern[kPatternHeadMode] intValue];
XAlignPaddingMode matchMode = [pattern[kPatternMatchMode] intValue];
XAlignPaddingMode tailMode = [pattern[kPatternTailMode] intValue];

NSDictionary * control = pattern[kPatternControl];
BOOL needTrim = [control[kPatternControlNeedTrim] boolValue];
BOOL needFormat = [control[kPatternControlNeedFormat] boolValue];
BOOL needFormatWhenFound = [control[kPatternControlNeedFormatWhenFound] boolValue];
BOOL needPadding = [control[kPatternControlNeedPadding] boolValue];
BOOL isMatchPadding = [control[kPatternControlIsMatchPadding] boolValue];
NSString * controlString = control[kPatternControlString];
NSString * paddingString = control[kPatternControlPaddingString];
NSString * foundString = control[kPatternControlFoundString];
NSString * format = control[kPatternControlFormat];
NSString * notFoundFormat = control[kPatternControlNotFoundFormat];
BOOL isOptional = [pattern[kPatternIsOptional] intValue];
XAlignPosition position = [pattern[kPatternPosition] intValue];
XAlignPaddingMode headMode = [pattern[kPatternHeadMode] intValue];
XAlignPaddingMode matchMode = [pattern[kPatternMatchMode] intValue];
XAlignPaddingMode tailMode = [pattern[kPatternTailMode] intValue];

NSDictionary * control = pattern[kPatternControl];
BOOL needTrim = [control[kPatternControlNeedTrim] boolValue];
BOOL needFormat = [control[kPatternControlNeedFormat] boolValue];
BOOL needFormatWhenFound = [control[kPatternControlNeedFormatWhenFound] boolValue];
BOOL needPadding = [control[kPatternControlNeedPadding] boolValue];
BOOL isMatchPadding = [control[kPatternControlIsMatchPadding] boolValue];
NSString * controlString = control[kPatternControlString];
NSString * paddingString = control[kPatternControlPaddingString];
NSString * foundString = control[kPatternControlFoundString];
NSString * format = control[kPatternControlFormat];
NSString * notFoundFormat = control[kPatternControlNotFoundFormat];

XAlignPattern * p = [[XAlignPattern alloc] init];

p.string = string;
p.headMode = headMode;
p.tailMode = tailMode;
p.matchMode = matchMode;
p.position = position;
p.isOptional = isOptional;
p.control = ^ NSString * ( NSUInteger padding, NSString * match )
{
Expand Down

0 comments on commit 9420d2b

Please sign in to comment.