3636using CodeSource ;
3737using DomainCommonExtensions . ArraysExtensions ;
3838using DomainCommonExtensions . CommonExtensions ;
39+ using DomainCommonExtensions . Helpers . Internal ;
3940using DomainCommonExtensions . Resources ;
4041using DomainCommonExtensions . Resources . Enums ;
4142using DomainCommonExtensions . Utilities . Ensure ;
@@ -117,7 +118,7 @@ public static bool IsValidEmail(this string email)
117118 /// <returns></returns>
118119 public static SecureString ToSecureString ( this string str )
119120 {
120- DomainEnsure . ThrowExceptionIfFuncIsTrue ( ExceptionType . ArgumentNullException ,
121+ DomainEnsure . ThrowExceptionIfFuncIsTrue ( ExceptionType . ArgumentNullException ,
121122 str . IsMissing , str , nameof ( str ) ) ;
122123
123124 var secureString = new SecureString ( ) ;
@@ -340,13 +341,17 @@ public static string TrimIfNotNull(this string value)
340341 /// Trim and reduce space from string
341342 /// </summary>
342343 /// <param name="value">Input value</param>
344+ /// <param name="reduceAllSpaces">
345+ /// Reduce/remove all spaces from source string data.
346+ /// Default value is 'false'.
347+ /// </param>
343348 /// <returns></returns>
344349 /// <remarks></remarks>
345- public static string TrimAndReduceSpace ( this string value )
350+ public static string TrimAndReduceSpace ( this string value , bool reduceAllSpaces = false )
346351 {
347352 if ( value . IfNullThenEmpty ( ) . IsMissing ( ) ) return null ;
348353
349- return Regex . Replace ( value , @"\s+" , " " ) . TrimIfNotNull ( ) ;
354+ return Regex . Replace ( value , @"\s+" , reduceAllSpaces . IsTrue ( ) ? "" : " " ) . TrimIfNotNull ( ) ;
350355 }
351356
352357 /// <summary>
@@ -504,11 +509,15 @@ public static string RemoveSpecialChars(this string str)
504509 /// Trim and replace special characters in string
505510 /// </summary>
506511 /// <param name="value">Input string</param>
512+ /// <param name="reduceAllSpaces">
513+ /// Reduce/remove all spaces from source string data.
514+ /// Default value is 'false'.
515+ /// </param>
507516 /// <returns></returns>
508517 /// <remarks></remarks>
509- public static string TrimAndReplaceSpecialCharacters ( this string value )
518+ public static string TrimAndReplaceSpecialCharacters ( this string value , bool reduceAllSpaces = false )
510519 {
511- return value . TrimAndReduceSpace ( ) . RemoveSpecialChars ( ) ;
520+ return value . TrimAndReduceSpace ( reduceAllSpaces ) . RemoveSpecialChars ( ) ;
512521 }
513522
514523 /// <summary>
@@ -1637,5 +1646,67 @@ public static string IfNotStartsWith(this string source, string searchValue, str
16371646 {
16381647 return source . StartsWith ( searchValue ) . IsFalse ( ) ? resultValue : source ;
16391648 }
1649+
1650+
1651+ /// <summary>
1652+ /// Check if string is in BASE32 format
1653+ /// </summary>
1654+ /// <param name="base32String">Encoded BASE32 string</param>
1655+ /// <returns></returns>
1656+ /// <remarks></remarks>
1657+ public static bool IsBase32String ( this string base32String )
1658+ {
1659+ if ( base32String . IsNullOrEmpty ( ) ) return false ;
1660+
1661+ base32String = base32String . TrimIfNotNull ( ) ;
1662+
1663+ return ( base32String . Length % 8 ) . IsZero ( ) &&
1664+ Regex . IsMatch ( base32String , RegularExpressions . BASE32 , RegexOptions . None ) ;
1665+ }
1666+
1667+ /// <summary>
1668+ /// Convert BASE32 string to byte[]
1669+ /// </summary>
1670+ /// <param name="base32String">Encoded BASE32 string.</param>
1671+ /// <returns>
1672+ /// A byte[].
1673+ /// </returns>
1674+ public static byte [ ] Base32ToBytes ( this string base32String )
1675+ {
1676+ DomainEnsure . IsNotNullOrEmptyArgNull ( base32String , nameof ( base32String ) ) ;
1677+
1678+ base32String = base32String . TrimEnd ( '=' ) ;
1679+ var byteCount = base32String . Length * 5 / 8 ;
1680+ var returnArray = new byte [ byteCount ] ;
1681+
1682+ byte curByte = 0 , bitsRemaining = 8 ;
1683+ var arrayIndex = 0 ;
1684+
1685+ foreach ( var character in base32String )
1686+ {
1687+ var charValue = Base32EncodingHelper . CharToInt32 ( character ) ;
1688+
1689+ int mask ;
1690+ if ( bitsRemaining > 5 )
1691+ {
1692+ mask = charValue << ( bitsRemaining - 5 ) ;
1693+ curByte = ( byte ) ( curByte | mask ) ;
1694+ bitsRemaining -= 5 ;
1695+ }
1696+ else
1697+ {
1698+ mask = charValue >> ( 5 - bitsRemaining ) ;
1699+ curByte = ( byte ) ( curByte | mask ) ;
1700+ returnArray [ arrayIndex ++ ] = curByte ;
1701+ curByte = ( byte ) ( charValue << ( 3 + bitsRemaining ) ) ;
1702+ bitsRemaining += 3 ;
1703+ }
1704+ }
1705+
1706+ if ( arrayIndex . Equals ( byteCount ) . IsFalse ( ) )
1707+ returnArray [ arrayIndex ] = curByte ;
1708+
1709+ return returnArray ;
1710+ }
16401711 }
16411712}
0 commit comments