开发小记

开发中的小笔记。记录开发中的点滴滴点。

批量替换json文件中的注释

备份至.bat

1
$ perl -i.bat -lpe 's/\/\/[^"]+$//g' xx

批量正则替换的另一种思路

1
2
3
4
#!/base/sh
find . -name '*.swift' | while read file; do
perl -i -lpe 's/if \(([^\)].*)\) \{/if $1 \{/g' $file
done

批量删除空格组成的空行中的空格

1
2
3
4
#!/base/sh
find . -name '*.swift' | while read file; do
sed -i '' 's/^[[:space:]][[:space:]]*$//g' $file
done

获取代码执行时间

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
#import <mach/mach_time.h>
static NSMutableDictionary *__times;
static inline void debug_start_of(NSString *key)
{
__times = __times ?: [NSMutableDictionary new];

uint64_t start = mach_absolute_time();
__times[key] = @(start);
}
static inline void debug_stop_of(NSString *key)
{
if (__times) {
uint64_t end = mach_absolute_time();
uint64_t start = [__times[key] unsignedLongLongValue];
if (start != 0) {
uint64_t elapsed = end - start;
mach_timebase_info_data_t info;
if (mach_timebase_info(&info) != KERN_SUCCESS) {
printf("mach_timebase_info failed\n");
}
uint64_t nanosecs = elapsed * info.numer / info.denom;
uint64_t millisecs = nanosecs / 1000000;

printf("[DEBUG code rt] %s:%llu ms\n", [key cStringUsingEncoding:NSUTF8StringEncoding], millisecs);
} else {
printf("[DEBUG code rt] %s error: no start\n", [key cStringUsingEncoding:NSUTF8StringEncoding]);
}
}
}

NAN

  • AB除余时,如果B == 0 则会造成NAN,truncatingRemainder(dividingBy:)swift的方法,也会造成此问题

  • NSUInteger 0 - 1时,运算结果为 NAN,iOS中有一个宏isnan(x)返回是否为nan。另外这里有一篇文章Objective-C 中 判断 NaN大致记录了一下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    //  math.h  #56

    #if defined(__GNUC__)
    # define HUGE_VAL __builtin_huge_val()
    # define HUGE_VALF __builtin_huge_valf()
    # define HUGE_VALL __builtin_huge_vall()
    # define NAN __builtin_nanf("0x7fc00000")
    #else
    # define HUGE_VAL 1e500
    # define HUGE_VALF 1e50f
    # define HUGE_VALL 1e5000L
    # define NAN __nan()
    #endif

  • 另外,NAN不能直接判断 == 需要调用下面的宏。否则会发生莫名其妙的问题

    1
    2
    3
    4
    5
    6
      //  math.h  #178

    #define isnan(x) \
    ( sizeof(x) == sizeof(float) ? __inline_isnanf((float)(x)) \
    : sizeof(x) == sizeof(double) ? __inline_isnand((double)(x)) \
    : __inline_isnanl((long double)(x)))