Skip to content

Instantly share code, notes, and snippets.

@dck-jp
Created May 27, 2014 02:46
Show Gist options
  • Save dck-jp/9f73846aa14411b93ebc to your computer and use it in GitHub Desktop.
Save dck-jp/9f73846aa14411b93ebc to your computer and use it in GitHub Desktop.
simify HereDocumentExtension.cs
/// <summary>
/// [Rules]
/// - all of lines (except first) require same indent
/// * indent = mix of tab and space is ok
/// - first line requires empty (only line break)
/// * linebreak = CR+LF or LF
///
/// [SpecialThanks]
/// this code is inspired by sayurin's code (http://git.io/iE1MhA)
///
/// [Usage]
/// var x = @"
/// test
/// test".AsHereDocSimpleType2();
/// var x = @"
/// test
/// test
/// ".AsHereDocSimpleType2();
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string AsHereDocSimpleType2(this string input)
{
return Regex.Replace(input,
@"^(?<sep>\r?\n)(?<indent>[ \t]*)((?<text>[^\r\n]*)\k<sep>\k<indent>)*(?<text>[^\r\n]+)?$",
m => string.Join(
m.Groups["sep"].Value,
m.Groups["text"].Captures.Cast<Capture>().Select(c => c.Value)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment