This repository has been archived by the owner on Dec 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
#2 : Low hanging fruit performance improvements from profiling session #1188
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,23 +3,56 @@ | |
|
||
using System; | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
using NuGet.Resources; | ||
|
||
namespace NuGet | ||
{ | ||
public static class PackageIdValidator | ||
{ | ||
internal const int MaxPackageIdLength = 100; | ||
private static readonly Regex _idRegex = new Regex(@"^\w+([_.-]\w+)*$", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); | ||
|
||
public static bool IsValidPackageId(string packageId) | ||
{ | ||
if (packageId == null) | ||
if (string.IsNullOrWhiteSpace(packageId)) | ||
{ | ||
throw new ArgumentNullException("packageId"); | ||
throw new ArgumentException("packageId"); | ||
} | ||
return _idRegex.IsMatch(packageId); | ||
|
||
// Rules: | ||
// Should start with a character | ||
// Can be followed by '.' or '-'. Cannot have 2 of these special characters consecutively. | ||
// Cannot end with '-' or '.' | ||
|
||
var firstChar = packageId[0]; | ||
if (!char.IsLetterOrDigit(firstChar) && firstChar != '_') | ||
{ | ||
// Should start with a char/digit/_. | ||
return false; | ||
} | ||
|
||
var lastChar = packageId[packageId.Length - 1]; | ||
if (lastChar == '-' || lastChar == '.') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the last char be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah. |
||
{ | ||
// Should not end with a '-' or '.'. | ||
return false; | ||
} | ||
|
||
for (int index = 1; index < packageId.Length - 1; index++) | ||
{ | ||
var ch = packageId[index]; | ||
if (!char.IsLetterOrDigit(ch) || ch != '-' || ch != '.') | ||
{ | ||
return false; | ||
} | ||
|
||
if (ch == '-' || ch == '.' && ch == packageId[index - 1]) | ||
{ | ||
// Cannot have two successive '-' or '.' in the name. | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static void ValidatePackageId(string packageId) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Text.RegularExpressions; | ||
using System.Linq; | ||
|
||
namespace NuGet | ||
{ | ||
|
@@ -35,7 +35,10 @@ public static bool IsValidLocalPath(string path) | |
{ | ||
try | ||
{ | ||
return Regex.IsMatch(path.Trim(), @"^[A-Za-z]:\\") && Path.IsPathRooted(path) && (path.IndexOfAny(_invalidPathChars) == -1); | ||
return char.IsLetter(path[0]) && | ||
path.StartsWith(path[0] + ":\\") && | ||
Path.IsPathRooted(path) && | ||
(path.IndexOfAny(_invalidPathChars) == -1); | ||
} | ||
catch | ||
{ | ||
|
@@ -59,7 +62,7 @@ public static bool IsValidUncPath(string path) | |
try | ||
{ | ||
Path.GetFullPath(path); | ||
return Regex.IsMatch(path.Trim(), @"^\\\\"); | ||
return path.Trim().StartsWith("\\\\"); | ||
} | ||
catch | ||
{ | ||
|
@@ -78,7 +81,18 @@ public static bool IsValidUrl(string url) | |
Uri result; | ||
|
||
// Make sure url starts with protocol:// because Uri.TryCreate() returns true for local and UNC paths even if badly formed. | ||
return Regex.IsMatch(url, @"^\w+://", RegexOptions.IgnoreCase) && Uri.TryCreate(url, UriKind.Absolute, out result); | ||
var urlParts = url.Split(new string[] { "://" }, 2, StringSplitOptions.None); | ||
if (urlParts == null || urlParts.Length != 2) | ||
{ | ||
return false; | ||
} | ||
|
||
if (urlParts[0].Any(c => !char.IsLetterOrDigit(c) && c != '_')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This repeats quite a lot There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nah..contexts are bit different. One is url validation, other package name validation etc. |
||
{ | ||
return false; | ||
} | ||
|
||
return Uri.TryCreate(url, UriKind.Absolute, out result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IsLetterOrDigit
returns true for_
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope. Returns false for
_
.