OLD | NEW |
| (Empty) |
1 /* | |
2 Copyright 2011 Google Inc | |
3 | |
4 Licensed under the Apache License, Version 2.0 (the "License"); | |
5 you may not use this file except in compliance with the License. | |
6 You may obtain a copy of the License at | |
7 | |
8 http://www.apache.org/licenses/LICENSE-2.0 | |
9 | |
10 Unless required by applicable law or agreed to in writing, software | |
11 distributed under the License is distributed on an "AS IS" BASIS, | |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 See the License for the specific language governing permissions and | |
14 limitations under the License. | |
15 */ | |
16 | |
17 using System; | |
18 using System.IO; | |
19 | |
20 namespace Google.Build.Utils.Text | |
21 { | |
22 /// <summary> | |
23 /// Text helper utility. | |
24 /// </summary> | |
25 public static class TextUtils | |
26 { | |
27 /// <summary> | |
28 /// Inserts the specified content in the place between the start and end
tag, which can be found in the | |
29 /// specified source text. | |
30 /// </summary> | |
31 /// <param name="sourceText">Text which contains the tags.</param> | |
32 /// <param name="startTag">The tag marking the start of the replacement
area.</param> | |
33 /// <param name="endTag">The tag marking the end of the replacement area
.</param> | |
34 /// <param name="content">The content to insert into the replacement are
a.</param> | |
35 /// <returns>[..] {startTag} content {endTag} [..]</returns> | |
36 public static string InsertIntoText(string sourceText, string startTag,
string endTag, string content) | |
37 { | |
38 return ReplaceInText(sourceText, startTag, endTag, old => content); | |
39 } | |
40 | |
41 /// <summary> | |
42 /// Replaces the text within the given tags using the specified replace
function. | |
43 /// </summary> | |
44 /// <param name="sourceText">The text containing the tags.</param> | |
45 /// <param name="startTag">The tag marking the start of the replacement
area.</param> | |
46 /// <param name="endTag">The tag marking the end of the replacement area
.</param> | |
47 /// <param name="replace">The function used to replace the tag content.<
/param> | |
48 /// <returns>The resulting text.</returns> | |
49 public static string ReplaceInText(string sourceText, | |
50 string startTag, | |
51 string endTag, | |
52 Func<string, string> replace) | |
53 { | |
54 // Find the start tag. | |
55 int startIndex = sourceText.IndexOf(startTag); | |
56 if (startIndex < 0) | |
57 { | |
58 throw new ArgumentException("Start Tag not found: " startTag); | |
59 } | |
60 startIndex = startTag.Length; | |
61 | |
62 // Find the end tag. | |
63 int endIndex = sourceText.IndexOf(endTag, startIndex); | |
64 if (endIndex < 0) | |
65 { | |
66 throw new ArgumentException("End Tag not found: " endTag); | |
67 } | |
68 | |
69 // Replace the text | |
70 string oldText = sourceText.Substring(startIndex, endIndex - startIn
dex).Trim('\r', '\n'); | |
71 string newText = replace(oldText); | |
72 return sourceText.Substring(0, startIndex) Environment.NewLine | |
73 newText Environment.NewLine | |
74 sourceText.Substring(endIndex); | |
75 } | |
76 | |
77 /// <summary> | |
78 /// Replaces the text within the given tags using the specified replace
function. | |
79 /// </summary> | |
80 /// <param name="file">The file to replace the text in.</param> | |
81 /// <param name="startTag">The tag marking the start of the replacement
area.</param> | |
82 /// <param name="endTag">The tag marking the end of the replacement area
.</param> | |
83 /// <param name="replace">The function used to replace the tag content.<
/param> | |
84 public static void ReplaceInFile(string file, string startTag, string en
dTag, Func<string, string> replace) | |
85 { | |
86 File.WriteAllText(file, ReplaceInText(File.ReadAllText(file), startT
ag, endTag, replace)); | |
87 } | |
88 | |
89 /// <summary> | |
90 /// Inserts the specified content in the place between the start and end
tag, which can be found in the | |
91 /// specified source text. | |
92 /// </summary> | |
93 /// <param name="file">The file containing the tags.</param> | |
94 /// <param name="startTag">The tag marking the start of the replacement
area.</param> | |
95 /// <param name="endTag">The tag marking the end of the replacement area
.</param> | |
96 /// <param name="content">The content to insert into the replacement are
a.</param> | |
97 /// <returns>[..] {startTag} content {endTag} [..]</returns> | |
98 public static void InsertIntoFile(string file, string startTag, string e
ndTag, string content) | |
99 { | |
100 File.WriteAllText(file, InsertIntoText(File.ReadAllText(file), start
Tag, endTag, content)); | |
101 } | |
102 | |
103 /// <summary> | |
104 /// Transforms the first letter of the string into uppercase. | |
105 /// </summary> | |
106 public static string ToUpperFirstChar(this string str) | |
107 { | |
108 if (string.IsNullOrEmpty(str)) | |
109 { | |
110 return str; | |
111 } | |
112 return Char.ToUpper(str[0]) str.Substring(1); | |
113 } | |
114 | |
115 /// <summary> | |
116 /// Finishes the specified string with the specified character. | |
117 /// </summary> | |
118 /// <param name="str">The sentence to end.</param> | |
119 /// <param name="endChar">The character with which this sentence should
end.</param> | |
120 public static string MakeSentence(this string str, char endChar) | |
121 { | |
122 string trimmed = str.TrimEnd(' ', '\r', '\n'); | |
123 | |
124 if (trimmed.EndsWith(endChar.ToString())) | |
125 { | |
126 return str; | |
127 } | |
128 return trimmed endChar str.Substring(trimmed.Length); | |
129 } | |
130 | |
131 /// <summary> | |
132 /// Joins the two specified lines into a common string. | |
133 /// </summary> | |
134 public static string JoinLines(string a, string b) | |
135 { | |
136 return a Environment.NewLine b; | |
137 } | |
138 } | |
139 } | |
OLD | NEW |