Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
nameofSEOKWONHONG committed Dec 27, 2024
1 parent 2ba6270 commit 87c2acd
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 236 deletions.
28 changes: 7 additions & 21 deletions src/XNumberExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ public static string xToPhoneNumber(this string str)
{
if (str.xIsEmpty()) return string.Empty;
if (str.Length < 11) throw new Exception("str length is less than 11");

var nation = string.Empty;
var head = string.Empty;
var body = string.Empty;
var tail = string.Empty;

string head;
string body;
string tail;

if (str.Length > 11)
{
nation = str.xSubstringFirst(2);
var nation = str.xSubstringFirst(2);
head = str.xSubstringMiddle(2, 3);
body = str.xSubstringMiddle(5, 4);
tail = str.Substring(9);
Expand All @@ -41,8 +40,8 @@ public static string xToPhoneNumber(this string str)

public static string xDisplayNumber<T>(this T val, ENUM_NUMBER_FORMAT_TYPE type, ENUM_VIEW_ALLOW_TYPE allow = ENUM_VIEW_ALLOW_TYPE.NotAllow) where T : struct
{
if (val.GetType() == typeof(DateTime)) throw new NotSupportedException("DateTime is not support.");
if (val.GetType() == typeof(float)) throw new NotSupportedException("float is not support.");
if (val is DateTime) throw new NotSupportedException("DateTime is not support.");
if (val is float) throw new NotSupportedException("float is not support.");

var result = type switch
{
Expand Down Expand Up @@ -114,20 +113,7 @@ private static string MakeRRNString<T>(T val, ENUM_VIEW_ALLOW_TYPE allow)
return string.Format("{0}-*******", val.ToString().xSubstringFirst(6));
}

public static string xSubstringMiddle(this string value, int fromLen, int getLen)
{
return value.Substring(fromLen, getLen);
}

public static string xSubstringFirst(this string value, int length)
{
return value.Substring(0, length);
}

public static string xSubstringLast(this string value, int length)
{
return value.Substring(value.Length - length, length);
}

public static T ToSum<T>(this IEnumerable<T> args) where T : INumber<T>
{
Expand Down
247 changes: 123 additions & 124 deletions src/XStringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ namespace eXtensionSharp
{
public static class XStringExtensions
{
/*
* Span변환의 장점 : 스택에 메모리 할당되므로 GC가 발생하지 않도록 해줌.
* Memory<T> (T:Class) -> Span<T> (T:Struct)로 변환하여 사용 가능
* new 대신 stackalloc 사용할 경우 GC 압력이 줄어듬.(struct type에 대하여, int, char, byte 등등)
* windows stack 최대 할당 용량은 1MB
*/

/// <summary>
/// substring
/// </summary>
Expand All @@ -38,8 +31,24 @@ public static string xSubstring(this string str, int startIndex, int length = 0)
{
if (str.xIsEmpty()) return string.Empty;
if (str.Length <= 0) return string.Empty;
if (length > 0) return str.AsSpan()[startIndex..(startIndex + length)].ToString();
return str.AsSpan()[startIndex..str.Length].ToString();
if (length > 0) return str[startIndex..(startIndex + length)].ToString();
return str[startIndex..str.Length].ToString();
}

/// <summary>
/// substring use span
/// </summary>
/// <param name="str"></param>
/// <param name="startIndex"></param>
/// <param name="length"></param>
/// <returns></returns>
public static ReadOnlySpan<char> xSlice(this string str, int startIndex, int length = 0)
{
if(str.xIsEmpty()) return ReadOnlySpan<char>.Empty;
if (str.Length <= 0) return ReadOnlySpan<char>.Empty;
if(length <=0) return ReadOnlySpan<char>.Empty;

return str.AsSpan(startIndex, length);
}

/// <summary>
Expand Down Expand Up @@ -140,14 +149,12 @@ public static byte[] xCompress(this string value, CompressionLevel level = Compr
{
var bytes = Encoding.Unicode.GetBytes(value);

using (var memoryStream = new MemoryStream())
{
using (var gzipStream = new GZipStream(memoryStream, level))
{
gzipStream.Write(bytes, 0, bytes.Length);
}
return memoryStream.GetBuffer();
}
using var memoryStream = new MemoryStream();
using (var gzipStream = new GZipStream(memoryStream, level))
{
gzipStream.Write(bytes, 0, bytes.Length);
}
return memoryStream.GetBuffer();
}

/// <summary>
Expand All @@ -157,20 +164,14 @@ public static byte[] xCompress(this string value, CompressionLevel level = Compr
/// <returns></returns>
public static string xUnCompress(this byte[] bytes)
{
using (var memoryStream = new MemoryStream(bytes))
{
using (var outputStream = new MemoryStream())
{
using (var decompressStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
decompressStream.CopyTo(outputStream);

var outputBytes = outputStream.ToArray();
return Encoding.Unicode.GetString(outputBytes);
}
}
}
}
using var memoryStream = new MemoryStream(bytes);
using var outputStream = new MemoryStream();
using var decompressStream = new GZipStream(memoryStream, CompressionMode.Decompress);
decompressStream.CopyTo(outputStream);

var outputBytes = outputStream.ToArray();
return Encoding.Unicode.GetString(outputBytes);
}

/// <summary>
/// gzip compression
Expand All @@ -182,37 +183,29 @@ public static async Task<byte[]> xCompressAsync(this string value, CompressionLe
{
var bytes = Encoding.Unicode.GetBytes(value);

using (var memoryStream = new MemoryStream())
{
using (var gzipStream = new GZipStream(memoryStream, level))
{
await gzipStream.WriteAsync(bytes, 0, bytes.Length);
}
return memoryStream.ToArray();
}
}
using var memoryStream = new MemoryStream();
await using (var gzipStream = new GZipStream(memoryStream, level))
{
await gzipStream.WriteAsync(bytes, 0, bytes.Length);
}
return memoryStream.ToArray();
}

/// <summary>
/// gzip uncompression
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static async Task<string> xUnCompressAsync(this byte[] bytes)
{
using (var memoryStream = new MemoryStream(bytes))
{
using (var outputStream = new MemoryStream())
{
using (var decompressStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
await decompressStream.CopyToAsync(outputStream);

var outputBytes = outputStream.ToArray();
return Encoding.Unicode.GetString(outputBytes);
}
}
}
}
{
using var memoryStream = new MemoryStream(bytes);
using var outputStream = new MemoryStream();
await using var decompressStream = new GZipStream(memoryStream, CompressionMode.Decompress);
await decompressStream.CopyToAsync(outputStream);

var outputBytes = outputStream.ToArray();
return Encoding.Unicode.GetString(outputBytes);
}

public static string xJoin(this string[] value, string separator)
{
Expand Down Expand Up @@ -271,27 +264,15 @@ public static string xToString(this byte[] bytes)
public static string xDistinct(this string str)
{
var hash = new HashSet<char>();
str.xForEach(item =>
{
if (!hash.Contains(item))
{
hash.Add(item);
}
});
str.xForEach(item => { hash.Add(item); });

return hash.xJoin(string.Empty);
}

public static string xDistinct(this IEnumerable<string> items)
{
var hash = new HashSet<string>();
items.xForEach(item =>
{
if (!hash.Contains(item))
{
hash.Add(item);
}
});
items.xForEach(item => { hash.Add(item); });

return hash.xJoin();
}
Expand Down Expand Up @@ -390,63 +371,81 @@ public static string xRandomString(this int size, bool lowerCase = false)
return lowerCase ? builder.ToString().ToLower() : builder.ToString();
}

private static string xToString(Expression expr)
// private static string xToString(Expression expr)
// {
// switch (expr.NodeType)
// {
// case ExpressionType.Lambda:
// //x => (Something), return only (Something), the Body
// return xToString(((LambdaExpression)expr).Body);
//
// case ExpressionType.Convert:
// case ExpressionType.ConvertChecked:
// //type casts are not important
// return xToString(((UnaryExpression)expr).Operand);
//
// case ExpressionType.Call:
// //method call can be an Indexer (get_Item),
// var callExpr = (MethodCallExpression)expr;
// if (callExpr.Method.Name == "get_Item")
// {
// //indexer call
// return xToString(callExpr.Object) + "[" +
// string.Join(",", callExpr.Arguments.Select(xToString)) + "]";
// }
// else
// {
// //method call
// var arguments = callExpr.Arguments.Select(xToString).ToArray();
// string target;
// if (callExpr.Method.IsDefined(typeof(ExtensionAttribute), false))
// {
// //extension method
// target = string.Join(".", arguments[0], callExpr.Method.Name);
// arguments = arguments.Skip(1).ToArray();
// }
// else if (callExpr.Object == null)
// {
// //static method
// target = callExpr.Method.Name;
// }
// else
// {
// //instance method
// target = string.Join(".", xToString(callExpr.Object), callExpr.Method.Name);
// }
//
// return target + "(" + string.Join(",", arguments) + ")";
// }
// case ExpressionType.MemberAccess:
// //property or field access
// var memberExpr = (MemberExpression)expr;
// if (memberExpr.Expression.Type.Name.Contains("<>")) //closure type, don't show it.
// return memberExpr.Member.Name;
// else
// return string.Join(".", xToString(memberExpr.Expression), memberExpr.Member.Name);
// }
//
// //by default, show the standard implementation
// return expr.ToString();
// }

public static string xSubstringMiddle(this string value, int fromLen, int getLen)
{
switch (expr.NodeType)
{
case ExpressionType.Lambda:
//x => (Something), return only (Something), the Body
return xToString(((LambdaExpression)expr).Body);

case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
//type casts are not important
return xToString(((UnaryExpression)expr).Operand);

case ExpressionType.Call:
//method call can be an Indexer (get_Item),
var callExpr = (MethodCallExpression)expr;
if (callExpr.Method.Name == "get_Item")
{
//indexer call
return xToString(callExpr.Object) + "[" +
string.Join(",", callExpr.Arguments.Select(xToString)) + "]";
}
else
{
//method call
var arguments = callExpr.Arguments.Select(xToString).ToArray();
string target;
if (callExpr.Method.IsDefined(typeof(ExtensionAttribute), false))
{
//extension method
target = string.Join(".", arguments[0], callExpr.Method.Name);
arguments = arguments.Skip(1).ToArray();
}
else if (callExpr.Object == null)
{
//static method
target = callExpr.Method.Name;
}
else
{
//instance method
target = string.Join(".", xToString(callExpr.Object), callExpr.Method.Name);
}

return target + "(" + string.Join(",", arguments) + ")";
}
case ExpressionType.MemberAccess:
//property or field access
var memberExpr = (MemberExpression)expr;
if (memberExpr.Expression.Type.Name.Contains("<>")) //closure type, don't show it.
return memberExpr.Member.Name;
else
return string.Join(".", xToString(memberExpr.Expression), memberExpr.Member.Name);
}
if(value.xIsEmpty()) return string.Empty;
return value.Substring(fromLen, getLen);
}

//by default, show the standard implementation
return expr.ToString();
public static string xSubstringFirst(this string value, int length)
{
if(value.xIsEmpty()) return string.Empty;
return value.Substring(0, length);
}

public static string xSubstringLast(this string value, int length)
{
if(value.xIsEmpty()) return string.Empty;
return value.Substring(value.Length - length, length);
}
}
}
Loading

0 comments on commit 87c2acd

Please sign in to comment.