+/**\r
+ Count the storage space of a Unicode string. \r
+\r
+ This function handles the Unicode string with NARROW_CHAR\r
+ and WIDE_CHAR control characters. NARROW_HCAR and WIDE_CHAR\r
+ does not count in the resultant output. If a WIDE_CHAR is\r
+ hit, then 2 Unicode character will consume an output storage\r
+ space with size of CHAR16 till a NARROW_CHAR is hit.\r
+\r
+ @param String The input string to be counted.\r
+ @param LimitLen Whether need to limit the string length.\r
+ @param MaxWidth The max length this function supported.\r
+ @param Offset The max index of the string can be show out. \r
+\r
+ @return Storage space for the input string.\r
+\r
+**/\r
+UINTN\r
+UefiLibGetStringWidth (\r
+ IN CHAR16 *String,\r
+ IN BOOLEAN LimitLen,\r
+ IN UINTN MaxWidth,\r
+ OUT UINTN *Offset\r
+ )\r
+{\r
+ UINTN Index;\r
+ UINTN Count;\r
+ UINTN IncrementValue;\r
+\r
+ if (String == NULL) {\r
+ return 0;\r
+ }\r
+\r
+ Index = 0;\r
+ Count = 0;\r
+ IncrementValue = 1;\r
+\r
+ do {\r
+ //\r
+ // Advance to the null-terminator or to the first width directive\r
+ //\r
+ for (;(String[Index] != NARROW_CHAR) && (String[Index] != WIDE_CHAR) && (String[Index] != 0);\r
+ Index++, Count = Count + IncrementValue) {\r
+ if (LimitLen && Count > MaxWidth) {\r
+ break;\r
+ }\r
+ }\r
+\r
+ //\r
+ // We hit the null-terminator, we now have a count\r
+ //\r
+ if (String[Index] == 0) {\r
+ break;\r
+ }\r
+\r
+ if (LimitLen && Count > MaxWidth) {\r
+ *Offset = Index - 1;\r
+ break;\r
+ }\r
+\r
+ //\r
+ // We encountered a narrow directive - strip it from the size calculation since it doesn't get printed\r
+ // and also set the flag that determines what we increment by.(if narrow, increment by 1, if wide increment by 2)\r
+ //\r
+ if (String[Index] == NARROW_CHAR) {\r
+ //\r
+ // Skip to the next character\r
+ //\r
+ Index++;\r
+ IncrementValue = 1;\r
+ } else {\r
+ //\r
+ // Skip to the next character\r
+ //\r
+ Index++;\r
+ IncrementValue = 2;\r
+ }\r
+ } while (String[Index] != 0);\r
+\r
+ //\r
+ // Increment by one to include the null-terminator in the size\r
+ //\r
+ if (!LimitLen) {\r
+ Count++;\r
+ }\r
+\r
+ return Count * sizeof (CHAR16);\r
+}\r
+\r