1 | #Region "Microsoft.VisualBasic::419efa23219b84576161d08b456aab68, Microsoft.VisualBasic.Core\Text\Paragraph.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Module Paragraph |
35 | ' |
36 | ' Function: SplitParagraph, Trim |
37 | ' |
38 | ' |
39 | ' /********************************************************************************/ |
40 | |
41 | #End Region |
42 | |
43 | Imports System.Runtime.CompilerServices |
44 | Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel |
45 | Imports Microsoft.VisualBasic.Language |
46 | Imports Microsoft.VisualBasic.Linq |
47 | |
48 | Namespace Text |
49 | |
50 | Public Module Paragraph |
51 | |
52 | ''' <summary> |
53 | ''' |
54 | ''' </summary> |
55 | ''' <param name="text$"></param> |
56 | ''' <param name="len%"></param> |
57 | ''' <returns></returns> |
58 | ''' <remarks> |
59 | ''' 假若长度分割落在单词内,则添加一个连接符,假如是空格或者标点符号,则不处理 |
60 | ''' </remarks> |
61 | <Extension> Public Iterator Function SplitParagraph(text$, len%) As IEnumerable(Of String) |
62 | Dim lines$() = text.LineTokens |
63 | |
64 | For Each i As SeqValue(Of String) In lines.SeqIterator |
65 | Dim line$ = +i |
66 | Dim s As New Value(Of String) |
67 | Dim left% = Scan0 + 1 |
68 | |
69 | Do While (s = Mid$(line$, left, len)).Length = len |
70 | If s.Value.Length = 0 Then |
71 | Exit Do ' 已经结束了 |
72 | Else |
73 | left += len |
74 | End If |
75 | |
76 | Dim nextLine$ = Mid(line, left, len) _ |
77 | .Replace(ASCII.TAB, " "c) |
78 | Dim part As NamedValue(Of String) = |
79 | nextLine.GetTagValue |
80 | |
81 | If String.IsNullOrEmpty(nextLine) Then |
82 | Exit Do |
83 | End If |
84 | |
85 | If Not String.IsNullOrEmpty(part.Name) Then ' 有空格 |
86 | s.Value = Trim((+s) & part.Name) |
87 | left += part.Name.Length + 1 |
88 | ElseIf nextLine.First = " "c Then ' 第一个字符是空格,则忽略掉 |
89 | left += 1 |
90 | Else |
91 | s.Value &= nextLine ' 是剩余的结束部分 |
92 | Yield Trim(+s) |
93 | s.Value = Nothing ' 必须要value值删除字符串,否则会重复出现最后一行 |
94 | Exit Do |
95 | End If |
96 | |
97 | Yield Trim(+s) |
98 | Loop |
99 | |
100 | If Not String.IsNullOrEmpty(+s) Then |
101 | Yield Trim(+s) |
102 | Else |
103 | If i.i <> lines.Length - 1 Then |
104 | Yield vbCrLf |
105 | End If |
106 | End If |
107 | Next |
108 | End Function |
109 | |
110 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
111 | Private Function Trim(s$) As String |
112 | If s Is Nothing Then |
113 | Return "" |
114 | Else |
115 | Return s.Trim(" ", ASCII.TAB) |
116 | End If |
117 | End Function |
118 | End Module |
119 | End Namespace |