Convert StringHelpers into extension methods, and merge into StringExtensions, change namespace of string extensions

This commit is contained in:
adelikat 2014-07-03 15:05:02 +00:00
parent d5e2808944
commit 467f8da2fb
10 changed files with 45 additions and 41 deletions

View File

@ -4,6 +4,7 @@ using System.IO;
using System.Reflection; using System.Reflection;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Emulation.Common.IEmulatorExtensions;
using BizHawk.Emulation.Cores.Nintendo.SNES; using BizHawk.Emulation.Cores.Nintendo.SNES;
@ -176,8 +177,8 @@ namespace BizHawk.Client.Common
int x = NumParentDirectories(path); int x = NumParentDirectories(path);
if (x > 0) if (x > 0)
{ {
int y = StringHelpers.HowMany(path, "..\\"); int y = path.HowMany("..\\");
int z = StringHelpers.HowMany(workingpath, "\\"); int z = workingpath.HowMany("\\");
if (y >= z) if (y >= z)
{ {
//Return drive letter only, working path must be absolute? //Return drive letter only, working path must be absolute?
@ -192,10 +193,10 @@ namespace BizHawk.Client.Common
public static int NumParentDirectories(string path) public static int NumParentDirectories(string path)
{ {
// determine the number of parent directories in path and return result // determine the number of parent directories in path and return result
int x = StringHelpers.HowMany(path, '\\'); int x = path.HowMany('\\');
if (x > 0) if (x > 0)
{ {
return StringHelpers.HowMany(path, "..\\"); return path.HowMany("..\\");
} }
return 0; return 0;

View File

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
@ -444,7 +445,7 @@ namespace BizHawk.Client.Common
continue; continue;
} }
var numColumns = StringHelpers.HowMany(line, '\t'); var numColumns = line.HowMany('\t');
int startIndex; int startIndex;
if (numColumns == 5) if (numColumns == 5)
{ {

View File

@ -1,8 +1,9 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using BizHawk.Common;
using Community.CsharpSqlite.SQLiteClient; using Community.CsharpSqlite.SQLiteClient;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Client.DBMan namespace BizHawk.Client.DBMan
{ {
public static class DirectoryScan public static class DirectoryScan

View File

@ -2,7 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace BizHawk.Common namespace BizHawk.Common.StringExtensions
{ {
public static class StringExtensions public static class StringExtensions
{ {
@ -13,20 +13,19 @@ namespace BizHawk.Common
public static string GetPrecedingString(this string str, string value) public static string GetPrecedingString(this string str, string value)
{ {
int index = str.IndexOf(value); var index = str.IndexOf(value);
if (index < 0) if (index < 0)
{ {
return null; return null;
} }
else if (index == 0)
if (index == 0)
{ {
return String.Empty; return string.Empty;
}
else
{
return str.Substring(0, index);
} }
return str.Substring(0, index);
} }
public static bool IsValidRomExtentsion(this string str, params string[] romExtensions) public static bool IsValidRomExtentsion(this string str, params string[] romExtensions)
@ -59,5 +58,24 @@ namespace BizHawk.Common
{ {
return options.All(opt => opt.ToLower() != str.ToLower()); return options.All(opt => opt.ToLower() != str.ToLower());
} }
public static int HowMany(this string str, char c)
{
return !string.IsNullOrEmpty(str) ? str.Count(t => t == c) : 0;
}
public static int HowMany(this string str, string s)
{
var count = 0;
for (var i = 0; i < (str.Length - s.Length); i++)
{
if (str.Substring(i, s.Length) == s)
{
count++;
}
}
return count;
}
} }
} }

View File

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using BizHawk.Common.StringExtensions;
// the HawkFile class is excessively engineered with the IHawkFileArchiveHandler to decouple the archive handling from the basic file handling. // the HawkFile class is excessively engineered with the IHawkFileArchiveHandler to decouple the archive handling from the basic file handling.
// This is so we could drop in an unamanged dearchiver library optionally later as a performance optimization without ruining the portability of the code. // This is so we could drop in an unamanged dearchiver library optionally later as a performance optimization without ruining the portability of the code.
// Also, we want to be able to use HawkFiles in BizHawk.Common withuot bringing in a large 7-zip dependency // Also, we want to be able to use HawkFiles in BizHawk.Common withuot bringing in a large 7-zip dependency

View File

@ -4,6 +4,8 @@ namespace BizHawk.Common
{ {
using System.Linq; using System.Linq;
using BizHawk.Common.StringExtensions;
/// <summary> /// <summary>
/// Includes helper functions to validate user input /// Includes helper functions to validate user input
/// </summary> /// </summary>
@ -101,7 +103,7 @@ namespace BizHawk.Common
/// </summary> /// </summary>
public static bool IsFixedPoint(string str) public static bool IsFixedPoint(string str)
{ {
return StringHelpers.HowMany(str, '.') <= 1 return str.HowMany('.') <= 1
&& str.All(IsFixedPoint); && str.All(IsFixedPoint);
} }
@ -118,7 +120,7 @@ namespace BizHawk.Common
/// </summary> /// </summary>
public static bool IsFloat(string str) public static bool IsFloat(string str)
{ {
return StringHelpers.HowMany(str, '.') <= 1 return str.HowMany('.') <= 1
&& IsFloat(str[0]) && IsFloat(str[0])
&& str.Substring(1).All(IsFixedPoint); && str.Substring(1).All(IsFixedPoint);
} }

View File

@ -2,29 +2,6 @@
namespace BizHawk.Common namespace BizHawk.Common
{ {
// TODO: these classes are worthless or need to be extensions, decide which
public static class StringHelpers
{
public static int HowMany(string str, char c)
{
return !string.IsNullOrEmpty(str) ? str.Count(t => t == c) : 0;
}
public static int HowMany(string str, string s)
{
var count = 0;
for (int i = 0; i < (str.Length - s.Length); i++)
{
if (str.Substring(i, s.Length) == s)
{
count++;
}
}
return count;
}
}
// TODO: put it in its own file // TODO: put it in its own file
public static class IntHelpers // TODO: a less lame name public static class IntHelpers // TODO: a less lame name
{ {

View File

@ -1,7 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using BizHawk.Common; using BizHawk.Common.StringExtensions;
namespace BizHawk.Emulation.Cores.Components.M68000 namespace BizHawk.Emulation.Cores.Components.M68000
{ {

View File

@ -1,9 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.IO; using System.IO;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
using BizHawk.Emulation.Common.Components; using BizHawk.Emulation.Common.Components;
using BizHawk.Emulation.Cores.Components.Z80; using BizHawk.Emulation.Cores.Components.Z80;