WIP Update lib to 2.37

This commit is contained in:
2017-10-20 20:02:52 +02:00
committed by BONNEVILLE Geoffroy
parent 9de9ae54da
commit d5b7845242
105 changed files with 9829 additions and 2410 deletions

View File

@@ -360,9 +360,9 @@ namespace ModernKeePassLib.Utility
}
/// <summary>
/// Split up a command-line into application and argument.
/// Split up a command line into application and argument.
/// </summary>
/// <param name="strCmdLine">Command-line to split.</param>
/// <param name="strCmdLine">Command line to split.</param>
/// <param name="strApp">Application path.</param>
/// <param name="strArgs">Arguments.</param>
public static void SplitCommandLine(string strCmdLine, out string strApp, out string strArgs)
@@ -501,7 +501,7 @@ namespace ModernKeePassLib.Utility
#if !KeePassLibSD
#if !ModernKeePassLib && !KeePassRT
if(excp.TargetSite != null)
strText += excp.TargetSite.ToString() + MessageService.NewLine;
strText += excp.TargetSite.ToString() + Environment.NewLine;
#endif
if(excp.Data != null)
@@ -671,7 +671,8 @@ namespace ModernKeePassLib.Utility
return DateTime.TryParse(str, out dt);
#else
try { dt = DateTime.Parse(str); return true; }
catch(Exception) { dt = DateTime.MinValue; return false; }
catch(Exception) { dt = DateTime.UtcNow; }
return false;
#endif
}
@@ -720,7 +721,7 @@ namespace ModernKeePassLib.Utility
/// <summary>
/// Removes all characters that are not valid XML characters,
/// according to http://www.w3.org/TR/xml/#charsets .
/// according to https://www.w3.org/TR/xml/#charsets .
/// </summary>
/// <param name="strText">Source text.</param>
/// <returns>Text containing only valid XML characters.</returns>
@@ -762,7 +763,7 @@ namespace ModernKeePassLib.Utility
return sb.ToString();
}
private static Regex m_rxNaturalSplit = null;
/* private static Regex g_rxNaturalSplit = null;
public static int CompareNaturally(string strX, string strY)
{
Debug.Assert(strX != null);
@@ -773,39 +774,31 @@ namespace ModernKeePassLib.Utility
if(NativeMethods.SupportsStrCmpNaturally)
return NativeMethods.StrCmpNaturally(strX, strY);
strX = strX.ToLower(); // Case-insensitive comparison
strY = strY.ToLower();
if(g_rxNaturalSplit == null)
g_rxNaturalSplit = new Regex(@"([0-9]+)", RegexOptions.Compiled);
if(m_rxNaturalSplit == null)
m_rxNaturalSplit = new Regex(@"([0-9]+)",
#if ModernKeePassLib || KeePassRT
RegexOptions.None);
#else
RegexOptions.Compiled);
#endif
string[] vPartsX = g_rxNaturalSplit.Split(strX);
string[] vPartsY = g_rxNaturalSplit.Split(strY);
string[] vPartsX = m_rxNaturalSplit.Split(strX);
string[] vPartsY = m_rxNaturalSplit.Split(strY);
for(int i = 0; i < Math.Min(vPartsX.Length, vPartsY.Length); ++i)
int n = Math.Min(vPartsX.Length, vPartsY.Length);
for(int i = 0; i < n; ++i)
{
string strPartX = vPartsX[i], strPartY = vPartsY[i];
int iPartCompare;
#if KeePassLibSD
ulong uX = 0, uY = 0;
try
{
uX = ulong.Parse(strPartX);
uY = ulong.Parse(strPartY);
ulong uX = ulong.Parse(strPartX);
ulong uY = ulong.Parse(strPartY);
iPartCompare = uX.CompareTo(uY);
}
catch(Exception) { iPartCompare = strPartX.CompareTo(strPartY); }
catch(Exception) { iPartCompare = string.Compare(strPartX, strPartY, true); }
#else
ulong uX, uY;
if(ulong.TryParse(strPartX, out uX) && ulong.TryParse(strPartY, out uY))
iPartCompare = uX.CompareTo(uY);
else iPartCompare = strPartX.CompareTo(strPartY);
else iPartCompare = string.Compare(strPartX, strPartY, true);
#endif
if(iPartCompare != 0) return iPartCompare;
@@ -814,6 +807,114 @@ namespace ModernKeePassLib.Utility
if(vPartsX.Length == vPartsY.Length) return 0;
if(vPartsX.Length < vPartsY.Length) return -1;
return 1;
} */
public static int CompareNaturally(string strX, string strY)
{
Debug.Assert(strX != null);
if(strX == null) throw new ArgumentNullException("strX");
Debug.Assert(strY != null);
if(strY == null) throw new ArgumentNullException("strY");
if(NativeMethods.SupportsStrCmpNaturally)
return NativeMethods.StrCmpNaturally(strX, strY);
int cX = strX.Length;
int cY = strY.Length;
if(cX == 0) return ((cY == 0) ? 0 : -1);
if(cY == 0) return 1;
char chFirstX = strX[0];
char chFirstY = strY[0];
bool bExpNum = ((chFirstX >= '0') && (chFirstX <= '9'));
bool bExpNumY = ((chFirstY >= '0') && (chFirstY <= '9'));
#if ModernKeePassLib
if (bExpNum != bExpNumY) return StringComparer.OrdinalIgnoreCase.Compare(strX, strY);
#else
if(bExpNum != bExpNumY) return string.Compare(strX, strY, true);
#endif
int pX = 0;
int pY = 0;
while((pX < cX) && (pY < cY))
{
Debug.Assert(((strX[pX] >= '0') && (strX[pX] <= '9')) == bExpNum);
Debug.Assert(((strY[pY] >= '0') && (strY[pY] <= '9')) == bExpNum);
int pExclX = pX + 1;
while(pExclX < cX)
{
char ch = strX[pExclX];
bool bChNum = ((ch >= '0') && (ch <= '9'));
if(bChNum != bExpNum) break;
++pExclX;
}
int pExclY = pY + 1;
while(pExclY < cY)
{
char ch = strY[pExclY];
bool bChNum = ((ch >= '0') && (ch <= '9'));
if(bChNum != bExpNum) break;
++pExclY;
}
string strPartX = strX.Substring(pX, pExclX - pX);
string strPartY = strY.Substring(pY, pExclY - pY);
bool bStrCmp = true;
if(bExpNum)
{
// 2^64 - 1 = 18446744073709551615 has length 20
if((strPartX.Length <= 19) && (strPartY.Length <= 19))
{
ulong uX, uY;
if(ulong.TryParse(strPartX, out uX) && ulong.TryParse(strPartY, out uY))
{
if(uX < uY) return -1;
if(uX > uY) return 1;
bStrCmp = false;
}
else { Debug.Assert(false); }
}
else
{
double dX, dY;
if(double.TryParse(strPartX, out dX) && double.TryParse(strPartY, out dY))
{
if(dX < dY) return -1;
if(dX > dY) return 1;
bStrCmp = false;
}
else { Debug.Assert(false); }
}
}
if(bStrCmp)
{
#if ModernKeePassLib
int c = StringComparer.OrdinalIgnoreCase.Compare(strPartX, strPartY);
#else
int c = string.Compare(strPartX, strPartY, true);
#endif
if(c != 0) return c;
}
bExpNum = !bExpNum;
pX = pExclX;
pY = pExclY;
}
if(pX >= cX)
{
Debug.Assert(pX == cX);
if(pY >= cY) { Debug.Assert(pY == cY); return 0; }
return -1;
}
Debug.Assert(pY == cY);
return 1;
}
public static string RemoveAccelerator(string strMenuText)
@@ -888,13 +989,12 @@ namespace ModernKeePassLib.Utility
public static bool IsHexString(string str, bool bStrict)
{
if(str == null) throw new ArgumentNullException("str");
if(str.Length == 0) return true;
foreach(char ch in str)
{
if((ch >= '0') && (ch <= '9')) continue;
if((ch >= 'a') && (ch <= 'z')) continue;
if((ch >= 'A') && (ch <= 'Z')) continue;
if((ch >= 'a') && (ch <= 'f')) continue;
if((ch >= 'A') && (ch <= 'F')) continue;
if(bStrict) return false;
@@ -907,8 +1007,31 @@ namespace ModernKeePassLib.Utility
return true;
}
public static bool IsHexString(byte[] pbUtf8, bool bStrict)
{
if(pbUtf8 == null) throw new ArgumentNullException("pbUtf8");
for(int i = 0; i < pbUtf8.Length; ++i)
{
byte bt = pbUtf8[i];
if((bt >= (byte)'0') && (bt <= (byte)'9')) continue;
if((bt >= (byte)'a') && (bt <= (byte)'f')) continue;
if((bt >= (byte)'A') && (bt <= (byte)'F')) continue;
if(bStrict) return false;
if((bt == (byte)' ') || (bt == (byte)'\t') ||
(bt == (byte)'\r') || (bt == (byte)'\n'))
continue;
return false;
}
return true;
}
#if !KeePassLibSD
private static readonly char[] m_vPatternPartsSep = new char[]{ '*' };
private static readonly char[] m_vPatternPartsSep = new char[] { '*' };
public static bool SimplePatternMatch(string strPattern, string strText,
StringComparison sc)
{
@@ -1252,7 +1375,7 @@ namespace ModernKeePassLib.Utility
return v;
}
private static readonly char[] m_vTagSep = new char[]{ ',', ';', ':' };
private static readonly char[] m_vTagSep = new char[] { ',', ';', ':' };
public static string TagsToString(List<string> vTags, bool bForDisplay)
{
if(vTags == null) throw new ArgumentNullException("vTags");
@@ -1625,5 +1748,16 @@ namespace ModernKeePassLib.Utility
return iCount;
}
internal static string ReplaceNulls(string str)
{
if(str == null) { Debug.Assert(false); return null; }
if(str.IndexOf('\0') < 0) return str;
// Replacing null characters by spaces is the
// behavior of Notepad (on Windows 10)
return str.Replace('\0', ' ');
}
}
}