Created
November 2, 2012 03:22
-
-
Save Icehunter/3998535 to your computer and use it in GitHub Desktop.
Example of wrapping a paragraph every nth character. Example of what I feel is Good/Better code; subjectively.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WrappingDemo.cs | |
// | |
// Created by Ryan Wilson. | |
using System; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
namespace WrappingDemoWPF | |
{ | |
public static class WrappingDemo | |
{ | |
/// <summary> | |
/// Good Code | |
/// Why is it good? Just like below; it's even shorter and fun to work with because of the linq involvment. | |
/// Based on user level however it may not be easy to change or understand. | |
/// The LINQ code however after testing on 50,000 lines of data performs faster than the "better" code below. | |
/// </summary> | |
/// <param name="data"> </param> | |
/// <param name="wrap"> </param> | |
/// <param name="type"> </param> | |
/// <returns> </returns> | |
public static string Wrap1(string data, int wrap, string type) | |
{ | |
/* PERFORMANCE :: | |
* On large data sets LINQ will process data at least 26% faster when extracting/splitting | |
*/ | |
switch (type) | |
{ | |
case "regex": | |
//OPTION #1 :: RegEx spliting and wrapping of paragraph every nth character | |
var pattern = String.Format("{0}{1}{2}", @"(?<=\G.{", wrap, "})"); | |
var regexSplit = Regex.Split(data, pattern, RegexOptions.Multiline); | |
return regexSplit.Aggregate("", (current, s) => current + s + Environment.NewLine); | |
case "linq": | |
//OPTION @2 :: LINQ splitting and wrapping of parahgrah every nth character | |
var wrapLimit = Enumerable.Range(0, (int) Math.Ceiling(data.Length/(decimal) wrap)); | |
var linqSplit = wrapLimit.Select(i => data.Substring(i*wrap, (i < wrapLimit.Count() - 1) ? wrap : data.Length - (i*wrap))).ToArray(); | |
return linqSplit.Aggregate("", (current, s) => current + s + Environment.NewLine); | |
} | |
return ""; | |
} | |
/// <summary> | |
/// Better Code | |
/// Why is it better? Because it works. It's easy to understand and change and manipulating data is always fun to work with. | |
/// Now better is actually subjective. It's better in that the average person can read it easily and modify it. | |
/// Performance wise however there is a degradation in solvetime based on a very large data set. | |
/// </summary> | |
/// <param name="data"> </param> | |
/// <param name="wrap"> </param> | |
/// <param name="type"> </param> | |
/// <returns> </returns> | |
public static string Wrap2(string data, int wrap, string type) | |
{ | |
switch (type) | |
{ | |
case "regex": | |
//OPTION #1 :: RegEx spliting and wrapping of paragraph every nth character | |
var regexResult = ""; | |
var pattern = String.Format("{0}{1}{2}", @"(?<=\G.{", wrap, "})"); | |
var options = RegexOptions.Multiline; | |
var regexSplit = Regex.Split(data, pattern, options); | |
foreach (var line in regexSplit) | |
{ | |
regexResult += line + Environment.NewLine; | |
} | |
return regexResult; | |
case "linq": | |
//OPTION @2 :: Not really LINQ but demonstrates an alternative way of wrapping/extracting. | |
var linqResult = ""; | |
var wrapLimit = (int) Math.Ceiling(data.Length/(decimal) wrap); | |
for (var i = 0; i < wrapLimit; i++) | |
{ | |
if (i < (wrapLimit - 1)) | |
{ | |
linqResult += data.Substring(i*wrap, wrap) + Environment.NewLine; | |
} | |
else | |
{ | |
linqResult += data.Substring(i*wrap, data.Length - (i*wrap)) + Environment.NewLine; | |
} | |
} | |
return linqResult; | |
} | |
return ""; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment