public static void Join(System.Text.StringBuilder sb, string s, IEnumerable<string> tokens) { int n = tokens.Count(); int c = tokens.Select(t => t.Length).Sum(); c += (n > 1) ? s.Length*n : 0; c += sb.Length; sb.EnsureCapacity(c); int i = 0; foreach (string t in tokens) { if (i > 0) { sb.Append(s); } sb.Append(t); i++; } }
private static void appendComponent(string component, int compIndex, int compSize, System.Text.StringBuilder buf) // Buffer to append the component. { for (; compIndex < component.Length; compIndex++) { char c = component[compIndex]; if (c == '/') { // Eliminate duplicate slashes. while ((compIndex < compSize) && (component[compIndex + 1] == '/')) { compIndex++; } // Only add a slash if following non-slash elements exist. if (compIndex < compSize) { buf.EnsureCapacity(buf.Length + 1); buf.Append('/'); } } else { buf.EnsureCapacity(buf.Length + 1); buf.Append(c); } } }
/* *----------------------------------------------------------------------------- * * getWinHomePath -- * * In the Windows file system, one type of absolute path follows this * regular expression: ^(//+[a-zA-Z]+/+[a-zA-Z]+) * * If "path" doesn't fit the pattern, then return 0. * If the stopEarly bool is true, then return the index of the first * non-slash character in path, as soon as we know that path fits the * pattern. Otherwise, return the index of the slash (or end of string) * following the entire absolute path. * * Results: * Returns an integer index in path. * * Side effects: * If "path" fits the pattern, and "stopEarly" is not chosen, the absolute * path is coppied (without extra slashes) to "absBuf". Otherwise, absBuf * is set to "". * *----------------------------------------------------------------------------- */ private static int getWinHomePath(string path, bool stopEarly, System.Text.StringBuilder absBuf) // Buffer to store side effect. { int pIndex, oldIndex, firstNonSlash; // The first 2 or more chars must be slashes. for (pIndex = 0; pIndex < path.Length; pIndex++) { if (path[pIndex] != '/') { break; } } if (pIndex < 2) { absBuf.Length = 0; return 0; } firstNonSlash = pIndex; // The next 1 or more chars may not be slashes. for (; pIndex < path.Length; pIndex++) { if (path[pIndex] == '/') { break; } } if (pIndex == firstNonSlash) { absBuf.Length = 0; return 0; } absBuf.EnsureCapacity(absBuf.Length + path.Length); absBuf.Append("//"); absBuf.Append(path.Substring(firstNonSlash, (pIndex) - (firstNonSlash))); // The next 1 or more chars must be slashes. oldIndex = pIndex; for (; pIndex < path.Length; pIndex++) { if (path[pIndex] != '/') { if (pIndex == oldIndex) { absBuf.Length = 0; return 0; } // We know that the path fits the pattern. if (stopEarly) { absBuf.Length = 0; return firstNonSlash; } firstNonSlash = pIndex; // Traverse the path until a new slash (or end of string) is found. // Return the index of the new slash. pIndex++; for (; pIndex < path.Length; pIndex++) { if (path[pIndex] == '/') { break; } } absBuf.Append('/'); absBuf.Append(path.Substring(firstNonSlash, (pIndex) - (firstNonSlash))); return pIndex; } } absBuf.Length = 0; return 0; }
private static int getWinAbsPath(string path, System.Text.StringBuilder absBuf) // Buffer to store side effect. { absBuf.Length = 0; if (path.Length < 1) { return 0; } absBuf.EnsureCapacity(absBuf.Length + path.Length); int colonIndex = beginsWithLetterColon(path); if (colonIndex > 0) { if (colonIndex > 2) { absBuf.Append(path.Substring(0, (3) - (0))); } else { absBuf.Append(path.Substring(0, (2) - (0))); } return colonIndex; } else { int absIndex = getWinHomePath(path, false, absBuf); if (absIndex > 0) { return absIndex; } else if (path[0] == '/') { int pIndex; for (pIndex = 1; pIndex < path.Length; pIndex++) { if (path[pIndex] != '/') { break; } } absBuf.Append("/"); return pIndex; } } return 0; }