1 struct TwoDigits 2 { 3 public readonly char First; 4 public readonly char Second; 5 6 public TwoDigits(char first, char second) 7 { 8 First = first; 9 Second = second;10 }11 }12 13 private static readonly TwoDigits[] DigitPairs;14 15 static Methods()16 {17 DigitPairs = new TwoDigits[100];18 for (var i=0; i < 100; ++i)19 DigitPairs[i] = new TwoDigits((char)('0' + (i / 10)), (char)+('0' + (i % 10)));20 }
是不是想法很高呢?
减少判断
原先的方法是这样的
1 static bool IsWhiteSpace(int c) 2 { 3 // per http://www.ietf.org/rfc/rfc4627.txt 4 // insignificant whitespace in JSON is defined as 5 // \u0020 - space 6 // \u0009 - tab 7 // \u000A - new line 8 // \u000D - carriage return 9 10 return11 c == 0x20 ||12 c == 0x09 ||13 c == 0x0A ||14 c == 0x0D;15 }
1 static bool IsWhiteSpace(int c) 2 { 3 // per http://www.ietf.org/rfc/rfc4627.txt 4 // insignificant whitespace in JSON is defined as 5 // \u0020 - space 6 // \u0009 - tab 7 // \u000A - new line 8 // \u000D - carriage return 9 10 return11 c < 0x21 && (12 c == 0x20 ||13 c == 0x09 ||14 c == 0x0A ||15 c == 0x0D);16 }