You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by xi...@apache.org on 2018/12/14 08:26:00 UTC

[incubator-weex] branch master updated: [iOS] Optimize get bundle type. (#1912)

This is an automated email from the ASF dual-hosted git repository.

xifang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-weex.git


The following commit(s) were added to refs/heads/master by this push:
     new 3a7f33a  [iOS] Optimize get bundle type. (#1912)
3a7f33a is described below

commit 3a7f33a699dd460e56a9ea80366117bef29f369a
Author: wqyfavor <wq...@gmail.com>
AuthorDate: Fri Dec 14 16:25:56 2018 +0800

    [iOS] Optimize get bundle type. (#1912)
---
 ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m | 48 +++++++++++++++---------
 1 file changed, 31 insertions(+), 17 deletions(-)

diff --git a/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m b/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
index 7fa9b28..093945b 100644
--- a/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
+++ b/ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
@@ -604,34 +604,50 @@ _Pragma("clang diagnostic pop") \
         // Fallback on earlier versions
         return bundleType;
     }
-    // trim like whiteSpace and newline charset
-    jsBundleString = [jsBundleString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
     
-    // use the top 100 characters match the bundleType
-    if (jsBundleString.length > 100) {
-        jsBundleString = [jsBundleString substringWithRange:NSMakeRange(0, 100)];
+    // find first character that is not space or new line character
+    NSCharacterSet* voidCharSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
+    NSUInteger length = [jsBundleString length];
+    NSUInteger validCharacter = 0;
+    while (validCharacter < length && [voidCharSet characterIsMember:[jsBundleString characterAtIndex:validCharacter]]) {
+        validCharacter ++;
     }
-    
-    if (!jsBundleString ) {
+    if (validCharacter >= length) {
+        return bundleType;
+    }
+    @try {
+        jsBundleString = [jsBundleString substringWithRange:NSMakeRange(validCharacter, MIN(100, length - validCharacter))];
+    }
+    @catch (NSException* e) {
+    }
+    if ([jsBundleString length] == 0) {
         return bundleType;
     }
+    
+    static NSRegularExpression* headerExp = nil;
+    static NSRegularExpression* vueExp = nil;
+    static NSRegularExpression* raxExp = nil;
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        headerExp = [NSRegularExpression regularExpressionWithPattern:@"^\\s*\\/\\/ *(\\{[^}]*\\}) *\\r?\\n" options:NSRegularExpressionCaseInsensitive error:NULL];
+        vueExp = [NSRegularExpression regularExpressionWithPattern:@"(use)(\\s+)(weex:vue)" options:NSRegularExpressionCaseInsensitive error:NULL];
+        raxExp = [NSRegularExpression regularExpressionWithPattern:@"(use)(\\s+)(weex:rax)" options:NSRegularExpressionCaseInsensitive error:NULL];
+    });
+    
     if ( [self _isParserByRegEx]) {
-        NSRegularExpression * regEx = [NSRegularExpression regularExpressionWithPattern:@"^\\s*\\/\\/ *(\\{[^}]*\\}) *\\r?\\n" options:NSRegularExpressionCaseInsensitive error:NULL];
-        NSTextCheckingResult *match = [regEx firstMatchInString:jsBundleString options:0 range:NSMakeRange(0, jsBundleString.length)];
+        NSTextCheckingResult *match = [headerExp firstMatchInString:jsBundleString options:0 range:NSMakeRange(0, jsBundleString.length)];
         if (match) {
             NSString* bundleTypeStr = [jsBundleString substringWithRange:match.range];
             bundleTypeStr = [bundleTypeStr stringByReplacingOccurrencesOfString:@"//" withString:@""];
             id vale = [WXUtility objectFromJSON:bundleTypeStr];
             bundleType = [vale objectForKey:@"framework"];
         }else{
-            NSRegularExpression * regEx = [NSRegularExpression regularExpressionWithPattern:@"(use)(\\s+)(weex:vue)" options:NSRegularExpressionCaseInsensitive error:NULL];
-            NSTextCheckingResult *match = [regEx firstMatchInString:jsBundleString options:0 range:NSMakeRange(0, jsBundleString.length)];
+            NSTextCheckingResult *match = [vueExp firstMatchInString:jsBundleString options:0 range:NSMakeRange(0, jsBundleString.length)];
             if (match) {
                 bundleType = [jsBundleString substringWithRange:match.range];
                 return bundleType;
             }
-            regEx = [NSRegularExpression regularExpressionWithPattern:@"(use)(\\s+)(weex:rax)" options:NSRegularExpressionCaseInsensitive error:NULL];
-            match = [regEx firstMatchInString:jsBundleString options:0 range:NSMakeRange(0, jsBundleString.length)];
+            match = [raxExp firstMatchInString:jsBundleString options:0 range:NSMakeRange(0, jsBundleString.length)];
             if (match) {
                 bundleType = [jsBundleString substringWithRange:match.range];
             }
@@ -642,14 +658,12 @@ _Pragma("clang diagnostic pop") \
         } else if ([jsBundleString hasPrefix:@"// { \"framework\": \"Rax\""] || [jsBundleString hasPrefix:@"// { \"framework\": \"rax\""] || [jsBundleString hasPrefix:@"// {\"framework\" : \"Rax\"}"] || [jsBundleString hasPrefix:@"// {\"framework\" : \"rax\"}"]) {
             bundleType = @"Rax";
         }else {
-            NSRegularExpression * regEx = [NSRegularExpression regularExpressionWithPattern:@"(use)(\\s+)(weex:vue)" options:NSRegularExpressionCaseInsensitive error:NULL];
-            NSTextCheckingResult *match = [regEx firstMatchInString:jsBundleString options:0 range:NSMakeRange(0, jsBundleString.length)];
+            NSTextCheckingResult *match = [vueExp firstMatchInString:jsBundleString options:0 range:NSMakeRange(0, jsBundleString.length)];
             if (match) {
                 bundleType = [jsBundleString substringWithRange:match.range];
                 return bundleType;
             }
-            regEx = [NSRegularExpression regularExpressionWithPattern:@"(use)(\\s+)(weex:rax)" options:NSRegularExpressionCaseInsensitive error:NULL];
-            match = [regEx firstMatchInString:jsBundleString options:0 range:NSMakeRange(0, jsBundleString.length)];
+            match = [raxExp firstMatchInString:jsBundleString options:0 range:NSMakeRange(0, jsBundleString.length)];
             if (match) {
                 bundleType = [jsBundleString substringWithRange:match.range];
             }