| 364 | } |
| 365 | |
| 366 | private static string EscapeQuotes(string toEscape) |
| 367 | { |
| 368 | // Convert strings with both quotes and ticks into: foo'"bar -> concat("foo'", '"', "bar") |
| 369 | if (toEscape.IndexOf("\"", StringComparison.OrdinalIgnoreCase) > -1 && toEscape.IndexOf("'", StringComparison.OrdinalIgnoreCase) > -1) |
| 370 | { |
| 371 | bool quoteIsLast = false; |
| 372 | if (toEscape.LastIndexOf("\"", StringComparison.OrdinalIgnoreCase) == toEscape.Length - 1) |
| 373 | { |
| 374 | quoteIsLast = true; |
| 375 | } |
| 376 | |
| 377 | List<string> substrings = new List<string>(toEscape.Split('\"')); |
| 378 | if (quoteIsLast && string.IsNullOrEmpty(substrings[substrings.Count - 1])) |
| 379 | { |
| 380 | // If the last character is a quote ('"'), we end up with an empty entry |
| 381 | // at the end of the list, which is unnecessary. We don't want to split |
| 382 | // ignoring *all* empty entries, since that might mask legitimate empty |
| 383 | // strings. Instead, just remove the empty ending entry. |
| 384 | substrings.RemoveAt(substrings.Count - 1); |
| 385 | } |
| 386 | |
| 387 | StringBuilder quoted = new StringBuilder("concat("); |
| 388 | for (int i = 0; i < substrings.Count; i++) |
| 389 | { |
| 390 | quoted.Append("\"").Append(substrings[i]).Append("\""); |
| 391 | if (i == substrings.Count - 1) |
| 392 | { |
| 393 | if (quoteIsLast) |
| 394 | { |
| 395 | quoted.Append(", '\"')"); |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | quoted.Append(")"); |
| 400 | } |
| 401 | } |
| 402 | else |
| 403 | { |
| 404 | quoted.Append(", '\"', "); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | return quoted.ToString(); |
| 409 | } |
| 410 | |
| 411 | // Escape string with just a quote into being single quoted: f"oo -> 'f"oo' |
| 412 | if (toEscape.IndexOf("\"", StringComparison.OrdinalIgnoreCase) > -1) |
| 413 | { |
| 414 | return string.Format(CultureInfo.InvariantCulture, "'{0}'", toEscape); |
| 415 | } |
| 416 | |
| 417 | // Otherwise return the quoted string |
| 418 | return string.Format(CultureInfo.InvariantCulture, "\"{0}\"", toEscape); |
| 419 | } |
| 420 | |
| 421 | private static string GetLongestSubstringWithoutSpace(string s) |
| 422 | { |