Introduction
Today, I heard someone say that this class (this method) has all the systems. It is better to use the system directly. Do you still write better than the system?
I have a question: the same code is written by people. Why must the system be the best?
Not much nonsense. directly add code
string.IsNullOrWhiteSpace(string value)
I believe everyone is familiar with this method.
But one day I opened Reflector and saw his source code, I was shocked ....
// String class public static bool IsNullOrWhiteSpace (string value) {if (value! = Null) {for (int I = 0; I <value. Length; I ++) {if (! Char. isWhiteSpace (value [I]) {return false ;}} return true ;}// char class public static bool IsWhiteSpace (char c) {if (IsLatin1 (c )) {return IsWhiteSpaceLatin1 (c);} return CharUnicodeInfo. isWhiteSpace (c);} private static bool IsLatin1 (char ch) {return (ch <= '\ x00ff');} private static bool IsWhiteSpaceLatin1 (char c) {if (c! = '') & (C <'\ t') | (c>' \ R') & (c! = '\ X00a0') & (c! = '\ X0085') {return false;} return true;} // CharUnicodeInfo class internal static bool IsWhiteSpace (char c) {switch (GetUnicodeCategory (c )) {case UnicodeCategory. spaceSeparator: case UnicodeCategory. lineSeparator: case UnicodeCategory. failed: return true;} return false;} public static UnicodeCategory GetUnicodeCategory (char ch) {return InternalGetUnicodeCategory (ch);} internal static UnicodeCategory InternalGetUnicodeCategory (int ch) {return (UnicodeCategory) internalGetCategoryValue (ch, 0);} internal static unsafe byte InternalGetCategoryValue (int ch, int offset) {ushort num = s_pCategoryLevel1Index [ch> 8]; num = s_pCategoryLevel1Index [num + (ch> 4) & 15)]; byte * numPtr = (byte *) (s_pCategoryLevel1Index + num ); byte num2 = numPtr [ch & 15]; return s_pCategoriesValue [(num2 * 2) + offset];}
There are a total of 8 related methods, and the fields used are not further explored.
At least five times must be determined when the method char. IsWhiteSpaceLatin1 (char c) is used at most.
Why is such a simple method so complicated?
Performance Optimization in C #. The last section in my article is about char processing.
Here is an optimization method for changing the space for time. Although it is for changing the space for time, there is not much space to waste, because char can only be 65536 characters long at most.
With this, I can implement
/// <Summary> indicates an array of blank characters /// </summary> private static bool [] _ WhiteChars = InitWhiteChars (); /// <summary> initialize _ WhiteChars /// </summary> /// <returns> </returns> private static bool [] InitWhiteChars () {var arr = new bool [char. maxValue]; for (int I = char. minValue; I <= char. maxValue; I ++) {if (char. isWhiteSpace (char) I) // if the character is a blank character {arr [I] = true; // set the value of the array corresponding to the character to true} return arr ;} /// <summary> indicates the specified word The string is null, empty, or only composed of blank characters. (Faster than the system method) /// </summary> /// <param name = "str"> string to be tested. </Param> // <returns> </returns> public static bool IsNullOrWhiteSpace (this string str) {if (str = null) {return true ;} var length = str. length; if (str. length = 0) {return true;} if (_ WhiteChars [str [0] = false) {return false;} for (int I = 1; I <length; I ++) {if (_ WhiteChars [str [I] = false) {return false ;}} return true ;}
Yes, that's right. There are only two methods, one of which is a static method, and only one operation is performed globally.
There is only one operation for the array index value. If the value is true, it indicates that it is a blank character... what a simple logic is.
I have not studied the system method too deeply. Unless the. CharUnicodeInfo. InternalGetCategoryValue method may change the returned results during running, my method will be the same as the system method's return value.
Similarly, write another code to judge char
/// <Summary> indicates whether the specified Unicode Character belongs to the white space category. (Faster than the system method) /// </summary> /// <param name = "str"> Unicode character to be calculated. </Param> // <returns> </returns> public static bool IsWhiteSpace (this char value) {return _ WhiteChars [value];}
Remarks
When I first learned. net, csdn was still very popular. I vaguely remember that there were a variety of attack stickers on the csdn forum, many of which challenged system methods.
One of the http://bbs.csdn.net/topics/360117794 attack stickers
That is, at that time, I knew that the system method was only written by people. As long as a person exists, there must be an outsider.
Technology is the same as ancient martial arts (I often compare the design pattern to the martial arts routine.), The so-called wenwu1wuwuwu2, said that the martial arts is better than the good or bad, and the technology is the same.
Learn more, study more, observe more, and test more. If you really understand a method and a class, you will naturally know how it works.
Do not blindly worship anyone, as long as it is XXX, it must be the best! With my N years of experience, this is the best! Not convincing! They are all programmers. Let's talk about code.
Advertisement
For more C # technical exchanges, please join the group: 5946699; Code: C # exchange
Welcome to C #, love C #, and learn C #. Come and learn from each other and make common progress.
The group owner is me. If you have any questions, @ me in the group. Let's discuss the research together.