Skip to content

Commit

Permalink
StartsWithInvariant/EndsWithInvariant/IndexOfInvariant/LastIndexOfInv…
Browse files Browse the repository at this point in the history
…ariant methods. Cleanup.
  • Loading branch information
andrewvk committed Jun 2, 2019
1 parent f9876c5 commit 9985bb8
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 115 deletions.
7 changes: 6 additions & 1 deletion CodeJam.Main/Readme.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
CodeJam 2.2.0-beta3 Release Notes
---------------------------------

What's new in 2.2.0
-----------------------
* StartsWithInvariant/EndsWithInvariant/IndexOfInvariant/LastIndexOfInvariant methods.
* Code cleanup.

What's new in 2.2.0-rc1
-------------------------
-----------------------
* Add deconstructor to IndexedItem<T>, used in WithIndex extension method.
* Code cleanup.

Expand Down
98 changes: 98 additions & 0 deletions CodeJam.Main/Strings/StringExtensions.ToXxx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,104 @@ public static string ToInvariantString<T>([NotNull] this T s) where T : IFormatt
public static string ToInvariantString<T>([NotNull] this T s, string format) where T : IFormattable =>
s.ToString(format, CultureInfo.InvariantCulture);

#if !LESSTHAN_NETCOREAPP20 && !LESSTHAN_NETSTANDARD20
/// <summary>
/// Determines whether the beginning of this string instance matches the specified string when compared using the
/// invariant culture.
/// </summary>
/// <param name="str">String to check.</param>
/// <param name="value">The string to compare.</param>
/// <returns><c>true</c> if this instance begins with value; otherwise, <c>false</c>. </returns>
public static bool StartsWithInvariant(this string str, string value) =>

This comment has been minimized.

Copy link
@NN---

NN--- Jun 6, 2019

Member

@andrewvk Please add [NotNull] annotation.

str.StartsWith(value, StringComparison.InvariantCulture);

/// <summary>
/// Reports the zero-based index of the first occurrence of the specified string in the <paramref name="str"/>
/// using the invariant culture.
/// </summary>
/// <param name="str">The string to check.</param>
/// <param name="value">The string to seek.</param>
/// <returns>
/// The index position of the value parameter if that string is found, or -1 if it is not. If value is <see cref="string.Empty"/>,
/// the return value is 0
/// </returns>
public static int IndexOfInvariant(this string str, string value) =>
str.IndexOf(value, StringComparison.InvariantCulture);

/// <summary>
/// Reports the zero-based index of the first occurrence of the specified string in the <paramref name="str"/>
/// using the invariant culture. Parameter specify the starting search position in the current string.
/// </summary>
/// <param name="str">The string to check.</param>
/// <param name="value">The string to seek.</param>
/// <param name="startIndex">The search starting position.</param>
/// <returns>
/// The index position of the value parameter if that string is found, or -1 if it is not. If value is <see cref="string.Empty"/>,
/// the return value is 0
/// </returns>
public static int IndexOfInvariant(this string str, string value, int startIndex) =>
str.IndexOf(value, startIndex, StringComparison.InvariantCulture);

/// <summary>
/// Reports the zero-based index of the first occurrence of the specified string in the <paramref name="str"/>
/// using the invariant culture. Parameters specify the starting search position in the current string and
/// the number of characters in the current string to search.
/// </summary>
/// <param name="str">The string to check.</param>
/// <param name="value">The string to seek.</param>
/// <param name="startIndex">The search starting position.</param>
/// <param name="count">The number of character positions to examine.</param>
/// <returns>
/// The index position of the value parameter if that string is found, or -1 if it is not. If value is <see cref="string.Empty"/>,
/// the return value is 0
/// </returns>
public static int IndexOfInvariant(this string str, string value, int startIndex, int count) =>
str.IndexOf(value, startIndex, count, StringComparison.InvariantCulture);

/// <summary>
/// Reports the zero-based index of the last occurrence of the specified string in the <paramref name="str"/>
/// using the invariant culture.
/// </summary>
/// <param name="str">The string to check.</param>
/// <param name="value">The string to seek.</param>
/// <returns>
/// The index position of the value parameter if that string is found, or -1 if it is not. If value is <see cref="string.Empty"/>,
/// the return value is 0
/// </returns>
public static int LastIndexOfInvariant(this string str, string value) =>
str.LastIndexOf(value, StringComparison.InvariantCulture);

/// <summary>
/// Reports the zero-based index of the last occurrence of the specified string in the <paramref name="str"/>
/// using the invariant culture. Parameter specify the starting search position in the current string.
/// </summary>
/// <param name="str">The string to check.</param>
/// <param name="value">The string to seek.</param>
/// <param name="startIndex">The search starting position.</param>
/// <returns>
/// The index position of the value parameter if that string is found, or -1 if it is not. If value is <see cref="string.Empty"/>,
/// the return value is 0
/// </returns>
public static int LastIndexOfInvariant(this string str, string value, int startIndex) =>
str.LastIndexOf(value, startIndex, StringComparison.InvariantCulture);

/// <summary>
/// Reports the zero-based index of the last occurrence of the specified string in the <paramref name="str"/>
/// using the invariant culture. Parameters specify the starting search position in the current string and
/// the number of characters in the current string to search.
/// </summary>
/// <param name="str">The string to check.</param>
/// <param name="value">The string to seek.</param>
/// <param name="startIndex">The search starting position.</param>
/// <param name="count">The number of character positions to examine.</param>
/// <returns>
/// The index position of the value parameter if that string is found, or -1 if it is not. If value is <see cref="string.Empty"/>,
/// the return value is 0
/// </returns>
public static int LastIndexOfInvariant(this string str, string value, int startIndex, int count) =>
str.LastIndexOf(value, startIndex, count, StringComparison.InvariantCulture);
#endif

#region DateTime
/// <summary>
/// Converts the string representation of a number in a specified style and culture-specific format to its
Expand Down
228 changes: 114 additions & 114 deletions CodeJam.Main/Targeting/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,39 +89,39 @@ public static bool GetIsPublic([NotNull] this Type type)
#endif
}

[MethodImpl(AggressiveInlining)]
public static bool GetIsNestedPublic([NotNull] this Type type)
{
#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
return type.GetTypeInfo().IsNestedPublic;
#else
return type.IsNestedPublic;
#endif
}

[MethodImpl(AggressiveInlining)]
public static bool GetIsFromLocalAssembly([NotNull] this Type type)
{
#if SILVERLIGHT
string assemblyName = type.GetAssembly().FullName;
#else
var assemblyName = type.GetAssembly().GetName().Name;
#endif

try
{
#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
Assembly.Load(new AssemblyName { Name = assemblyName });
#else
Assembly.Load(assemblyName);
#endif
return true;
}
catch
{
return false;
}
}
// [MethodImpl(AggressiveInlining)]
// public static bool GetIsNestedPublic([NotNull] this Type type)
// {
//#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
// return type.GetTypeInfo().IsNestedPublic;
//#else
// return type.IsNestedPublic;
//#endif
// }

// [MethodImpl(AggressiveInlining)]
// public static bool GetIsFromLocalAssembly([NotNull] this Type type)
// {
//#if SILVERLIGHT
// string assemblyName = type.GetAssembly().FullName;
//#else
// var assemblyName = type.GetAssembly().GetName().Name;
//#endif

// try
// {
//#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
// Assembly.Load(new AssemblyName { Name = assemblyName });
//#else
// Assembly.Load(assemblyName);
//#endif
// return true;
// }
// catch
// {
// return false;
// }
// }

[MethodImpl(AggressiveInlining)]
public static bool GetIsGenericType([NotNull] this Type type)
Expand Down Expand Up @@ -184,15 +184,15 @@ public static bool GetContainsGenericParameters([NotNull] this Type type)
#endif
}

[MethodImpl(AggressiveInlining)]
public static bool GetIsDefined([NotNull] this Type type, [NotNull] Type attributeType)
{
#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
return type.GetTypeInfo().IsDefined(attributeType);
#else
return Attribute.IsDefined(type, attributeType);
#endif
}
// [MethodImpl(AggressiveInlining)]
// public static bool GetIsDefined([NotNull] this Type type, [NotNull] Type attributeType)
// {
//#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
// return type.GetTypeInfo().IsDefined(attributeType);
//#else
// return Attribute.IsDefined(type, attributeType);
//#endif
// }

[MethodImpl(AggressiveInlining)]
public static bool GetIsDefined([NotNull] this Type type, [NotNull] Type attributeType, bool inherit)
Expand All @@ -214,78 +214,78 @@ public static bool GetIsArray([NotNull] this Type type)
#endif
}

[MethodImpl(AggressiveInlining)]
public static PropertyInfo GetGetProperty([NotNull] this Type type, [NotNull] string propertyName)
{
#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
return type.GetTypeInfo().GetProperty(propertyName);
#else
return type.GetProperty(propertyName);
#endif
}

[MethodImpl(AggressiveInlining)]
public static T GetPropertyValue<T>([NotNull] this Type type, [NotNull] string propertyName, object target)
{
#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
var property = type.GetTypeInfo().GetDeclaredProperty(propertyName);
return (T)property.GetValue(target);
#else
return (T)type.InvokeMember(propertyName, BindingFlags.GetProperty, null, target, null);
#endif
}

[MethodImpl(AggressiveInlining)]
public static void SetPropertyValue(
[NotNull] this Type type, [NotNull] string propertyName, object target, object value)
{
#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
var property = type.GetTypeInfo().GetDeclaredProperty(propertyName);
property.SetValue(target, value);
#else
type.InvokeMember(propertyName, BindingFlags.SetProperty, null, target, new[] { value });
#endif
}

[MethodImpl(AggressiveInlining)]
public static T GetFieldValue<T>([NotNull] this Type type, [NotNull] string fieldName, object target)
{
#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
var field = type.GetTypeInfo().GetDeclaredField(fieldName);
return (T)field.GetValue(target);
#else
return (T)type.InvokeMember(fieldName, BindingFlags.GetField | BindingFlags.GetProperty, null, target, null);
#endif
}

[MethodImpl(AggressiveInlining)]
public static void SetFieldValue([NotNull] this Type type, [NotNull] string fieldName, object target, object value)
{
#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
var field = type.GetTypeInfo().GetDeclaredField(fieldName);
if (field != null)
{
field.SetValue(target, value);
}
else
{
type.SetPropertyValue(fieldName, target, value);
}
#else
type.InvokeMember(fieldName, BindingFlags.SetField | BindingFlags.SetProperty, null, target, new[] { value });
#endif
}

[MethodImpl(AggressiveInlining)]
public static void InvokeMethod<T>([NotNull] this Type type, [NotNull] string methodName, object target, T value)
{
#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
var method = type.GetTypeInfo().GetDeclaredMethod(methodName);
method.Invoke(target, new object[] { value });
#else
type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, target, new object[] { value });
#endif
}
// [MethodImpl(AggressiveInlining)]
// public static PropertyInfo GetGetProperty([NotNull] this Type type, [NotNull] string propertyName)
// {
//#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
// return type.GetTypeInfo().GetProperty(propertyName);
//#else
// return type.GetProperty(propertyName);
//#endif
// }

// [MethodImpl(AggressiveInlining)]
// public static T GetPropertyValue<T>([NotNull] this Type type, [NotNull] string propertyName, object target)
// {
//#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
// var property = type.GetTypeInfo().GetDeclaredProperty(propertyName);
// return (T)property.GetValue(target);
//#else
// return (T)type.InvokeMember(propertyName, BindingFlags.GetProperty, null, target, null);
//#endif
// }

// [MethodImpl(AggressiveInlining)]
// public static void SetPropertyValue(
// [NotNull] this Type type, [NotNull] string propertyName, object target, object value)
// {
//#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
// var property = type.GetTypeInfo().GetDeclaredProperty(propertyName);
// property.SetValue(target, value);
//#else
// type.InvokeMember(propertyName, BindingFlags.SetProperty, null, target, new[] { value });
//#endif
// }

// [MethodImpl(AggressiveInlining)]
// public static T GetFieldValue<T>([NotNull] this Type type, [NotNull] string fieldName, object target)
// {
//#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
// var field = type.GetTypeInfo().GetDeclaredField(fieldName);
// return (T)field.GetValue(target);
//#else
// return (T)type.InvokeMember(fieldName, BindingFlags.GetField | BindingFlags.GetProperty, null, target, null);
//#endif
// }

// [MethodImpl(AggressiveInlining)]
// public static void SetFieldValue([NotNull] this Type type, [NotNull] string fieldName, object target, object value)
// {
//#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
// var field = type.GetTypeInfo().GetDeclaredField(fieldName);
// if (field != null)
// {
// field.SetValue(target, value);
// }
// else
// {
// type.SetPropertyValue(fieldName, target, value);
// }
//#else
// type.InvokeMember(fieldName, BindingFlags.SetField | BindingFlags.SetProperty, null, target, new[] { value });
//#endif
// }

// [MethodImpl(AggressiveInlining)]
// public static void InvokeMethod<T>([NotNull] this Type type, [NotNull] string methodName, object target, T value)
// {
//#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
// var method = type.GetTypeInfo().GetDeclaredMethod(methodName);
// method.Invoke(target, new object[] { value });
//#else
// type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, target, new object[] { value });
//#endif
// }


#if LESSTHAN_NETSTANDARD20 || LESSTHAN_NETCOREAPP20
Expand Down
1 change: 1 addition & 0 deletions CodeJam.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=jitted/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=jitting/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Kurtosis/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=LESSTHAN/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=memoize/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Memoized/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=multithreaded/@EntryIndexedValue">True</s:Boolean>
Expand Down

0 comments on commit 9985bb8

Please sign in to comment.