Last active
November 11, 2018 20:44
-
-
Save jackmott/e752ba2f8471ad3d7f1b6c82c06233bb to your computer and use it in GitHub Desktop.
word wrap for monogame
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
public static string Wrap(string s, int width, Func<string, int> widthMeasure) | |
{ | |
StringBuilder builder = new StringBuilder(s.Length); | |
var text = s.AsSpan(); | |
int start = 0; | |
int len = 0; | |
for (int i = 0; i < text.Length; i++) { | |
if (text[i] == ' ') | |
{ | |
if (widthMeasure(text.Slice(start, i-start).ToString()) <= width) | |
{ | |
len = i - start; | |
} | |
else | |
{ | |
builder.AppendLine(text.Slice(start,len).ToString()); | |
start = i; | |
len = 0; | |
} | |
} | |
} | |
builder.AppendLine(text.Slice(start).ToString()); | |
return builder.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment