-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.cs
72 lines (61 loc) · 2.18 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Romulus
{
public static class Utils
{
public static string[] ParseGeminiLink(string line)
{
var linkRegex = new Regex(@"=>\s*([^\s]*)(.*)");
string[] array = new string[2];
if (linkRegex.IsMatch(line))
{
Match match = linkRegex.Match(line);
array[0] = match.Groups[1].ToString().Trim();
array[1] = match.Groups[2].ToString().Trim();
//if display text is empty, use url
if (array[1] == "")
{
array[1] = array[0];
}
}
else
{
//isnt a link, return null,null
array[0] = null;
array[1] = null;
}
return array;
}
//normalise tabs to spaces
public static string TabsToSpaces(string start)
{
return start.Replace("\t", " ");
}
/// <summary>
/// from https://gist.github.com/anderssonjohan/660952
/// </summary>
/// <param name="text"></param>
/// <param name="maxLineLength"></param>
/// <returns></returns>
///
public static List<string> WordWrap(string text, int maxLineLength)
{
var list = new List<string>();
int currentIndex;
var lastWrap = 0;
var whitespace = new[] { ' ', '\r', '\n', '\t' };
do
{
currentIndex = lastWrap + maxLineLength > text.Length ? text.Length : (text.LastIndexOfAny(new[] { ' ', ',', '.', '?', '!', ':', ';', '-', '\n', '\r', '\t' }, Math.Min(text.Length - 1, lastWrap + maxLineLength)) + 1);
if (currentIndex <= lastWrap)
currentIndex = Math.Min(lastWrap + maxLineLength, text.Length);
list.Add(text.Substring(lastWrap, currentIndex - lastWrap).Trim(whitespace));
lastWrap = currentIndex;
} while (currentIndex < text.Length);
return list;
}
}
}