11// -----------------------------------------------------------------------
22// <copyright file="ExtensionMethods.cs" company="Conglomo">
3- // Copyright 2020-2023 Conglomo Limited. Please see LICENSE.md for license details.
3+ // Copyright 2020-2024 Conglomo Limited. Please see LICENSE.md for license details.
44// </copyright>
55// -----------------------------------------------------------------------
66
@@ -62,7 +62,11 @@ public static CompareOptions AsCompareOptions(this RenderingParameters parameter
6262 /// <param name="value">The substring to count.</param>
6363 /// <param name="stringComparison">The string comparison type.</param>
6464 /// <returns>The number of occurrences of the substring within the string.</returns>
65- public static int CountOccurrences ( this string text , string value , StringComparison stringComparison )
65+ public static int CountOccurrences (
66+ this string text ,
67+ string value ,
68+ StringComparison stringComparison
69+ )
6670 {
6771 // Clean up the text and value
6872 text = $ " { text . Trim ( ) } ";
@@ -94,7 +98,8 @@ public static string EncodeCsvField(this string field, char separator = ',')
9498 StringBuilder sb = new StringBuilder ( field ) ;
9599
96100 // Fields with leading/trailing whitespace must be embedded in double quotes
97- bool embedInQuotes = sb . Length > 0 && ( sb [ 0 ] == ' ' || sb [ 0 ] == '\t ' || sb [ ^ 1 ] == ' ' || sb [ ^ 1 ] == '\t ' ) ;
101+ bool embedInQuotes =
102+ sb . Length > 0 && ( sb [ 0 ] == ' ' || sb [ 0 ] == '\t ' || sb [ ^ 1 ] == ' ' || sb [ ^ 1 ] == '\t ' ) ;
98103
99104 // If we have not yet found a reason to embed in quotes
100105 if ( ! embedInQuotes )
@@ -122,7 +127,12 @@ public static string EncodeCsvField(this string field, char separator = ',')
122127 /// <param name="approximatePosition">The approximate position of this occurrence. This should be an underestimate at worst.</param>
123128 /// <param name="stringComparison">The string comparison type.</param>
124129 /// <returns>The occurrence number of the substring within the string.</returns>
125- public static int GetOccurrence ( this string text , string value , int approximatePosition , StringComparison stringComparison )
130+ public static int GetOccurrence (
131+ this string text ,
132+ string value ,
133+ int approximatePosition ,
134+ StringComparison stringComparison
135+ )
126136 {
127137 // Clean up the text and value
128138 text = $ " { text . Trim ( ) } ";
@@ -157,7 +167,8 @@ public static int GetOccurrence(this string text, string value, int approximateP
157167 /// - brackets, see https://goto.bible/Acts.19_41/SBLGNT (SBL Greek New Testament)
158168 /// NOTE: The input must be directly from a translation.
159169 /// </returns>
160- public static string GetVerseNumber ( this string line ) => line . Contains ( ' ' ) ? line [ ..line . IndexOf ( ' ' ) ] . Trim ( ) : string . Empty ;
170+ public static string GetVerseNumber ( this string line ) =>
171+ line . Contains ( ' ' ) ? line [ ..line . IndexOf ( ' ' ) ] . Trim ( ) : string . Empty ;
161172
162173 /// <summary>
163174 /// Determines whether this is a valid verse number.
@@ -183,7 +194,11 @@ public static bool IsValidVerseNumber(this string verseNumber)
183194 public static bool MatchesHighlightedVerses ( this string verseNumber , string [ ] highlightedVerses )
184195 {
185196 // Ensure that the verse number is valid, and there are highlighted verses
186- if ( string . IsNullOrWhiteSpace ( verseNumber ) || ! highlightedVerses . Any ( ) || ! verseNumber . IsValidVerseNumber ( ) )
197+ if (
198+ string . IsNullOrWhiteSpace ( verseNumber )
199+ || highlightedVerses is [ ]
200+ || ! verseNumber . IsValidVerseNumber ( )
201+ )
187202 {
188203 return false ;
189204 }
@@ -196,7 +211,7 @@ public static bool MatchesHighlightedVerses(this string verseNumber, string[] hi
196211 {
197212 string [ ] verseNumbers = verseNumber . Split ( '-' , StringSplitOptions . RemoveEmptyEntries ) ;
198213 return verseNumbers . First ( ) . MatchesHighlightedVerses ( highlightedVerses )
199- || verseNumbers . Last ( ) . MatchesHighlightedVerses ( highlightedVerses ) ;
214+ || verseNumbers . Last ( ) . MatchesHighlightedVerses ( highlightedVerses ) ;
200215 }
201216
202217 // We need to pad the numbers to account for letters
@@ -233,7 +248,9 @@ public static bool MatchesHighlightedVerses(this string verseNumber, string[] hi
233248 else if ( highlightedVerses [ i ] == "-" && i > 0 && i < highlightedVerses . Length - 1 )
234249 {
235250 // Check inside this range
236- bool matches = string . CompareOrdinal ( verseNumber , highlightedVerses [ i - 1 ] ) > 0 && string . CompareOrdinal ( verseNumber , highlightedVerses [ i + 1 ] ) < 0 ;
251+ bool matches =
252+ string . CompareOrdinal ( verseNumber , highlightedVerses [ i - 1 ] ) > 0
253+ && string . CompareOrdinal ( verseNumber , highlightedVerses [ i + 1 ] ) < 0 ;
237254 if ( matches )
238255 {
239256 return matches ;
@@ -261,19 +278,23 @@ public static bool MatchesHighlightedVerses(this string verseNumber, string[] hi
261278 /// <remarks>
262279 /// Full-width square brackets are replaced with regular square brackets.
263280 /// </remarks>
264- public static string RenderItalics ( this string line , string italicsTag = "em" )
265- => line . Replace ( "[[" , "<pre>" ) . Replace ( "]]" , "</pre>" )
266- . Replace ( "[" , $ "<{ italicsTag } >") . Replace ( "]" , $ "</{ italicsTag } >")
267- . Replace ( "<pre>" , "[[" ) . Replace ( "</pre>" , "]]" )
268- . Replace ( "[" , "[" ) . Replace ( "]" , "]" ) ;
281+ public static string RenderItalics ( this string line , string italicsTag = "em" ) =>
282+ line . Replace ( "[[" , "<pre>" )
283+ . Replace ( "]]" , "</pre>" )
284+ . Replace ( "[" , $ "<{ italicsTag } >")
285+ . Replace ( "]" , $ "</{ italicsTag } >")
286+ . Replace ( "<pre>" , "[[" )
287+ . Replace ( "</pre>" , "]]" )
288+ . Replace ( "[" , "[" )
289+ . Replace ( "]" , "]" ) ;
269290
270291 /// <summary>
271292 /// Strips the HTML tags from the string.
272293 /// </summary>
273294 /// <param name="input">The input string.</param>
274295 /// <returns>The input string without HTML tags.</returns>
275- public static string StripHtml ( this string input )
276- => HtmlTagRegex ( ) . Replace ( input , string . Empty ) ;
296+ public static string StripHtml ( this string input ) =>
297+ HtmlTagRegex ( ) . Replace ( input , string . Empty ) ;
277298
278299 /// <summary>
279300 /// Renders the supplied words in normal type.
@@ -285,9 +306,11 @@ public static string StripHtml(this string input)
285306 /// <remarks>
286307 /// Full-width square brackets are replaced with regular square brackets.
287308 /// </remarks>
288- public static string StripItalics ( this string line )
289- => line . Replace ( "[" , string . Empty ) . Replace ( "]" , string . Empty )
290- . Replace ( "[" , "[" ) . Replace ( "]" , "]" ) ;
309+ public static string StripItalics ( this string line ) =>
310+ line . Replace ( "[" , string . Empty )
311+ . Replace ( "]" , string . Empty )
312+ . Replace ( "[" , "[" )
313+ . Replace ( "]" , "]" ) ;
291314
292315 /// <summary>
293316 /// Gets a unique name for the translation.
@@ -297,24 +320,50 @@ public static string StripItalics(this string line)
297320 /// <returns>
298321 /// The unique name.
299322 /// </returns>
300- public static string UniqueName ( this Translation translation , IEnumerable < Translation > translations )
323+ public static string UniqueName (
324+ this Translation translation ,
325+ IEnumerable < Translation > translations
326+ )
301327 {
302- IEnumerable < Translation > enumerable = translations as Translation [ ] ?? translations . ToArray ( ) ;
303- if ( enumerable . Count ( t => string . Equals ( t . Name , translation . Name , StringComparison . OrdinalIgnoreCase ) ) <= 1 )
328+ IEnumerable < Translation > enumerable =
329+ translations as Translation [ ] ?? translations . ToArray ( ) ;
330+ if (
331+ enumerable . Count ( t =>
332+ string . Equals ( t . Name , translation . Name , StringComparison . OrdinalIgnoreCase )
333+ ) <= 1
334+ )
304335 {
305336 return translation . Name ;
306337 }
307- else if ( enumerable . Count ( t => string . Equals ( t . Name , translation . Name , StringComparison . OrdinalIgnoreCase ) && t . Year == translation . Year && t . Year > 0 ) == 1 )
338+ else if (
339+ enumerable . Count ( t =>
340+ string . Equals ( t . Name , translation . Name , StringComparison . OrdinalIgnoreCase )
341+ && t . Year == translation . Year
342+ && t . Year > 0
343+ ) == 1
344+ )
308345 {
309346 return $ "{ translation . Name } ({ translation . Year } )";
310347 }
311- else if ( enumerable . Count ( t => string . Equals ( t . Name , translation . Name , StringComparison . OrdinalIgnoreCase ) && t . Language == translation . Language ) == 1 )
348+ else if (
349+ enumerable . Count ( t =>
350+ string . Equals ( t . Name , translation . Name , StringComparison . OrdinalIgnoreCase )
351+ && t . Language == translation . Language
352+ ) == 1
353+ )
312354 {
313355 // English translation names (i.e. King James Version) have priority
314- return translation . Language == "English" ? translation . Name : $ "{ translation . Language } { translation . Name } ";
356+ return translation . Language == "English"
357+ ? translation . Name
358+ : $ "{ translation . Language } { translation . Name } ";
315359 }
316- else if ( enumerable . Count ( t => string . Equals ( t . Name , translation . Name , StringComparison . OrdinalIgnoreCase )
317- && t . Dialect == translation . Dialect && ! string . IsNullOrWhiteSpace ( t . Dialect ) ) == 1 )
360+ else if (
361+ enumerable . Count ( t =>
362+ string . Equals ( t . Name , translation . Name , StringComparison . OrdinalIgnoreCase )
363+ && t . Dialect == translation . Dialect
364+ && ! string . IsNullOrWhiteSpace ( t . Dialect )
365+ ) == 1
366+ )
318367 {
319368 return $ "{ translation . Name } ({ translation . Dialect } )";
320369 }
@@ -338,6 +387,9 @@ public static string UniqueName(this Translation translation, IEnumerable<Transl
338387 /// <remarks>
339388 /// This includes support for verses with letters, hypenated verses, and verses enclosed in brackets.
340389 /// </remarks>
341- [ GeneratedRegex ( @"([\[]((([0-9]+[a-z]?)-([0-9]+[a-z]?))|([0-9]+[a-z]?))[\]])|((([0-9]+[a-z]?)-([0-9]+[a-z]?))|([0-9]+[a-z]?))" , RegexOptions . Compiled ) ]
390+ [ GeneratedRegex (
391+ @"([\[]((([0-9]+[a-z]?)-([0-9]+[a-z]?))|([0-9]+[a-z]?))[\]])|((([0-9]+[a-z]?)-([0-9]+[a-z]?))|([0-9]+[a-z]?))" ,
392+ RegexOptions . Compiled
393+ ) ]
342394 private static partial Regex VerseNumberRegex ( ) ;
343395}
0 commit comments