Skip to content

Commit

Permalink
Add StringExtensions and Parse utilizing CultureInfo.InvariantCulture
Browse files Browse the repository at this point in the history
Adds static methods of the form Parse.<TypeName>(string str) that use
CultureInfo.InvariantCulture. These are to be used when parsing strings.
It's still safe (from the CA1304/CA1305 perspective) to use the ToDecimal
extension method for decimals.

Adds string extension methods for common operations that will now require
CultureInfo.InvariantCulture. These are to be used when converting values
to strings, such as ToStringInvariant()/ToStringInvariant(format), but also
useful for searching within strings, StartsWithInvariant, EndsWithInvariant
 and IndexOfInvariant.
  • Loading branch information
mchandschuh authored and Martin-Molinero committed Aug 16, 2019
1 parent 703f915 commit c7df4e9
Show file tree
Hide file tree
Showing 46 changed files with 716 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Algorithm.CSharp/QuantConnect.Algorithm.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
<Reference Include="RDotNet.NativeLibrary, Version=1.6.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\R.NET.Community.1.6.5\lib\net40\RDotNet.NativeLibrary.dll</HintPath>
</Reference>
<Reference Include="StringInterpolationBridge, Version=0.9.1.0, Culture=neutral, PublicKeyToken=761dcf67ba0bf74d, processorArchitecture=MSIL">
<HintPath>..\packages\StringInterpolationBridgeStrong.0.9.1\lib\net40\StringInterpolationBridge.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
Expand Down
1 change: 1 addition & 0 deletions Algorithm.CSharp/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
<package id="NodaTime" version="1.3.4" targetFramework="net452" />
<package id="QuantConnect.pythonnet" version="1.0.5.20" targetFramework="net452" />
<package id="R.NET.Community" version="1.6.5" targetFramework="net452" />
<package id="StringInterpolationBridgeStrong" version="0.9.1" targetFramework="net452" />
</packages>
3 changes: 3 additions & 0 deletions Algorithm.Framework/QuantConnect.Algorithm.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
<Reference Include="NodaTime, Version=1.3.0.0, Culture=neutral, PublicKeyToken=4226afe0d9b296d1, processorArchitecture=MSIL">
<HintPath>..\packages\NodaTime.1.3.4\lib\net35-Client\NodaTime.dll</HintPath>
</Reference>
<Reference Include="StringInterpolationBridge, Version=0.9.1.0, Culture=neutral, PublicKeyToken=761dcf67ba0bf74d, processorArchitecture=MSIL">
<HintPath>..\packages\StringInterpolationBridgeStrong.0.9.1\lib\net40\StringInterpolationBridge.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
Expand Down
1 change: 1 addition & 0 deletions Algorithm.Framework/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
<package id="Microsoft.NetFramework.Analyzers" version="2.9.3" targetFramework="net452" />
<package id="NodaTime" version="1.3.4" targetFramework="net452" />
<package id="QuantConnect.pythonnet" version="1.0.5.20" targetFramework="net452" />
<package id="StringInterpolationBridgeStrong" version="0.9.1" targetFramework="net452" />
</packages>
5 changes: 5 additions & 0 deletions Algorithm.Python/QuantConnect.Algorithm.Python.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@
<Analyzer Include="..\packages\Microsoft.NetFramework.Analyzers.2.9.3\analyzers\dotnet\cs\Microsoft.NetFramework.Analyzers.dll" />
<Analyzer Include="..\packages\Microsoft.NetFramework.Analyzers.2.9.3\analyzers\dotnet\cs\Microsoft.NetFramework.CSharp.Analyzers.dll" />
</ItemGroup>
<ItemGroup>
<Reference Include="StringInterpolationBridge, Version=0.9.1.0, Culture=neutral, PublicKeyToken=761dcf67ba0bf74d, processorArchitecture=MSIL">
<HintPath>..\packages\StringInterpolationBridgeStrong.0.9.1\lib\net40\StringInterpolationBridge.dll</HintPath>
</Reference>
</ItemGroup>
<PropertyGroup>
<IsWindows>false</IsWindows>
<IsWindows Condition="'$(OS)' == 'Windows_NT'">true</IsWindows>
Expand Down
1 change: 1 addition & 0 deletions Algorithm.Python/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<package id="Microsoft.NetCore.Analyzers" version="2.9.3" targetFramework="net452" />
<package id="Microsoft.NetFramework.Analyzers" version="2.9.3" targetFramework="net452" />
<package id="QuantConnect.pythonnet" version="1.0.5.20" targetFramework="net452" />
<package id="StringInterpolationBridgeStrong" version="0.9.1" targetFramework="net452" />
</packages>
3 changes: 3 additions & 0 deletions Algorithm/QuantConnect.Algorithm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
<Reference Include="NodaTime, Version=1.3.0.0, Culture=neutral, PublicKeyToken=4226afe0d9b296d1, processorArchitecture=MSIL">
<HintPath>..\packages\NodaTime.1.3.4\lib\net35-Client\NodaTime.dll</HintPath>
</Reference>
<Reference Include="StringInterpolationBridge, Version=0.9.1.0, Culture=neutral, PublicKeyToken=761dcf67ba0bf74d, processorArchitecture=MSIL">
<HintPath>..\packages\StringInterpolationBridgeStrong.0.9.1\lib\net40\StringInterpolationBridge.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
Expand Down
1 change: 1 addition & 0 deletions Algorithm/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
<package id="NodaTime" version="1.3.4" targetFramework="net452" />
<package id="QuantConnect.pythonnet" version="1.0.5.20" targetFramework="net452" />
<package id="StringInterpolationBridgeStrong" version="0.9.1" targetFramework="net452" />
</packages>
3 changes: 3 additions & 0 deletions AlgorithmFactory/QuantConnect.AlgorithmFactory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<Reference Include="NodaTime, Version=1.3.0.0, Culture=neutral, PublicKeyToken=4226afe0d9b296d1, processorArchitecture=MSIL">
<HintPath>..\packages\NodaTime.1.3.4\lib\net35-Client\NodaTime.dll</HintPath>
</Reference>
<Reference Include="StringInterpolationBridge, Version=0.9.1.0, Culture=neutral, PublicKeyToken=761dcf67ba0bf74d, processorArchitecture=MSIL">
<HintPath>..\packages\StringInterpolationBridgeStrong.0.9.1\lib\net40\StringInterpolationBridge.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down
1 change: 1 addition & 0 deletions AlgorithmFactory/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<package id="Microsoft.NetFramework.Analyzers" version="2.9.3" targetFramework="net452" />
<package id="NodaTime" version="1.3.4" targetFramework="net452" />
<package id="QuantConnect.pythonnet" version="1.0.5.20" targetFramework="net452" />
<package id="StringInterpolationBridgeStrong" version="0.9.1" targetFramework="net452" />
</packages>
3 changes: 3 additions & 0 deletions Api/QuantConnect.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
<Reference Include="RestSharp, Version=106.6.10.0, Culture=neutral, PublicKeyToken=598062e77f915f75, processorArchitecture=MSIL">
<HintPath>..\packages\RestSharp.106.6.10\lib\net452\RestSharp.dll</HintPath>
</Reference>
<Reference Include="StringInterpolationBridge, Version=0.9.1.0, Culture=neutral, PublicKeyToken=761dcf67ba0bf74d, processorArchitecture=MSIL">
<HintPath>..\packages\StringInterpolationBridgeStrong.0.9.1\lib\net40\StringInterpolationBridge.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
Expand Down
1 change: 1 addition & 0 deletions Api/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
<package id="NodaTime" version="1.3.4" targetFramework="net452" />
<package id="RestSharp" version="106.6.10" targetFramework="net452" />
<package id="StringInterpolationBridgeStrong" version="0.9.1" targetFramework="net452" />
<package id="WebSocketSharpFork" version="1.0.4.0" targetFramework="net45" />
</packages>
3 changes: 3 additions & 0 deletions Brokerages/QuantConnect.Brokerages.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@
<Reference Include="RestSharp, Version=106.6.10.0, Culture=neutral, PublicKeyToken=598062e77f915f75, processorArchitecture=MSIL">
<HintPath>..\packages\RestSharp.106.6.10\lib\net452\RestSharp.dll</HintPath>
</Reference>
<Reference Include="StringInterpolationBridge, Version=0.9.1.0, Culture=neutral, PublicKeyToken=761dcf67ba0bf74d, processorArchitecture=MSIL">
<HintPath>..\packages\StringInterpolationBridgeStrong.0.9.1\lib\net40\StringInterpolationBridge.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.ComponentModel.DataAnnotations" />
Expand Down
1 change: 1 addition & 0 deletions Brokerages/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<package id="NodaTime" version="1.3.4" targetFramework="net452" />
<package id="QuantConnect.IBAutomater" version="1.0.4" targetFramework="net452" />
<package id="RestSharp" version="106.6.10" targetFramework="net452" />
<package id="StringInterpolationBridgeStrong" version="0.9.1" targetFramework="net452" />
<package id="System.Net.Http" version="4.3.3" targetFramework="net452" />
<package id="WebSocketSharpFork" version="1.0.4.0" targetFramework="net452" />
</packages>
112 changes: 112 additions & 0 deletions Common/Parse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Globalization;

namespace QuantConnect
{
/// <summary>
/// Provides methods for parsing strings using <see cref="CultureInfo.InvariantCulture"/>
/// </summary>
public static class Parse
{
/// <summary>
/// Parses the provided value as a <see cref="System.TimeSpan"/> using <see cref="System.TimeSpan.Parse(string,IFormatProvider)"/>
/// with <see cref="CultureInfo.InvariantCulture"/>
/// </summary>
public static TimeSpan TimeSpan(string value)
{
return System.TimeSpan.Parse(value, CultureInfo.InvariantCulture);
}

/// <summary>
/// Parses the provided value as a <see cref="System.DateTime"/> using <see cref="System.DateTime.Parse(string,IFormatProvider)"/>
/// with <see cref="CultureInfo.InvariantCulture"/>
/// </summary>
public static DateTime DateTime(string value)
{
return System.DateTime.Parse(value, CultureInfo.InvariantCulture);
}

/// <summary>
/// Parses the provided value as a <see cref="System.DateTime"/> using <see cref="System.DateTime.ParseExact(string,string,IFormatProvider)"/>
/// with the specified <paramref name="format"/> and <see cref="CultureInfo.InvariantCulture"/>
/// </summary>
public static DateTime DateTimeExact(string value, string format)
{
return System.DateTime.ParseExact(value, format, CultureInfo.InvariantCulture);
}

/// <summary>
/// Parses the provided value as a <see cref="System.DateTime"/> using <see cref="System.DateTime.ParseExact(string,string,IFormatProvider)"/>
/// with the specified <paramref name="format"/>, <paramref name="dateTimeStyles"/> and <see cref="CultureInfo.InvariantCulture"/>
/// </summary>
public static DateTime DateTimeExact(string value, string format, DateTimeStyles dateTimeStyles)
{
return System.DateTime.ParseExact(value, format, CultureInfo.InvariantCulture, dateTimeStyles);
}

/// <summary>
/// Parses the provided value as a <see cref="double"/> using <see cref="CultureInfo.InvariantCulture"/>
/// </summary>
public static double Double(string value)
{
return double.Parse(value, CultureInfo.InvariantCulture);
}

/// <summary>
/// Parses the provided value as a <see cref="decimal"/> using <see cref="CultureInfo.InvariantCulture"/>
/// </summary>
public static decimal Decimal(string value)
{
return decimal.Parse(value, CultureInfo.InvariantCulture);
}

/// <summary>
/// Parses the provided value as a <see cref="decimal"/> using the specified <paramref name="numberStyles"/>
/// and <see cref="CultureInfo.InvariantCulture"/>
/// </summary>
public static decimal Decimal(string value, NumberStyles numberStyles)
{
return decimal.Parse(value, numberStyles, CultureInfo.InvariantCulture);
}

/// <summary>
/// Parses the provided value as a <see cref="int"/> using <see cref="CultureInfo.InvariantCulture"/>
/// </summary>
public static int Int(string value)
{
return int.Parse(value, CultureInfo.InvariantCulture);
}

/// <summary>
/// Parses the provided value as a <see cref="long"/> using <see cref="CultureInfo.InvariantCulture"/>
/// </summary>
public static long Long(string value)
{
return long.Parse(value, CultureInfo.InvariantCulture);
}

/// <summary>
/// Parses the provided value as a <see cref="long"/> using <see cref="CultureInfo.InvariantCulture"/>
/// and the specified <paramref name="numberStyles"/>
/// </summary>
public static long Long(string value, NumberStyles numberStyles)
{
return long.Parse(value, numberStyles, CultureInfo.InvariantCulture);
}
}
}
5 changes: 5 additions & 0 deletions Common/QuantConnect.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
<Reference Include="QLNet, Version=1.9.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\QLNet.1.9.2\lib\net45\QLNet.dll</HintPath>
</Reference>
<Reference Include="StringInterpolationBridge, Version=0.9.1.0, Culture=neutral, PublicKeyToken=761dcf67ba0bf74d, processorArchitecture=MSIL">
<HintPath>..\packages\StringInterpolationBridgeStrong.0.9.1\lib\net40\StringInterpolationBridge.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Configuration" />
Expand Down Expand Up @@ -696,6 +699,8 @@
<Compile Include="Statistics\TradeStatistics.cs" />
<Compile Include="Statistics\Statistics.cs" />
<Compile Include="RealTimeSynchronizedTimer.cs" />
<Compile Include="StringExtensions.cs" />
<Compile Include="Parse.cs" />
<Compile Include="Symbol.cs" />
<Compile Include="Statistics\Trade.cs" />
<Compile Include="Statistics\TradeBuilder.cs" />
Expand Down
Loading

0 comments on commit c7df4e9

Please sign in to comment.