Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Use ToInvariantString() for all numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorg committed Nov 5, 2015
1 parent 96fc759 commit 6bd667e
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/mindtouch.core/AmazonS3/AmazonS3Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public void PutFile(string path, AmazonS3FileHandle fileHandle) {
var p = _rootPlug.AtPath(path);
if(fileHandle.TimeToLive.HasValue) {
var expiration = fileHandle.Expiration ?? DateTime.UtcNow.Add(fileHandle.TimeToLive.Value);
p = p.WithHeader(EXPIRE, expiration.ToEpoch().ToString())
.WithHeader(TTL, fileHandle.TimeToLive.Value.TotalSeconds.ToString());
p = p.WithHeader(EXPIRE, expiration.ToEpoch().ToInvariantString())
.WithHeader(TTL, fileHandle.TimeToLive.Value.TotalSeconds.ToInvariantString());
_expirationEntries.SetOrUpdate(path, expiration, fileHandle.TimeToLive.Value);
}
var request = DreamMessage.Ok(fileHandle.MimeType, fileHandle.Size, fileHandle.Stream);
Expand Down
2 changes: 1 addition & 1 deletion src/mindtouch.core/DreamCoreUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal static XDoc ExecuteCommand(Plug env, DreamHeaders headers, XDoc cmd) {
case "pipe":
return ExecutePipe(env, headers, cmd);
default:
throw new DreamException(string.Format("unregonized script command: " + cmd.Name.ToString()));
throw new DreamException(string.Format("unregonized script command: " + cmd.Name));
}
} catch(Exception e) {
return new XException(e);
Expand Down
5 changes: 3 additions & 2 deletions src/mindtouch.db/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -261,14 +262,14 @@ public IEnumerable<string> Parse(IEnumerable<string> options) {
break;
}
// is it a bundled option?
if(f == "-" && this.options.TryGetValue(n[0].ToString(), out p2)) {
if(f == "-" && this.options.TryGetValue(n[0].ToString(CultureInfo.InvariantCulture), out p2)) {
int i = 0;
do {
if(p2.OptionValue != OptionValue.None)
throw new InvalidOperationException(
string.Format("Unsupported using bundled option '{0}' that requires a value", n[i]));
p2.Action(n);
} while(++i < n.Length && this.options.TryGetValue(n[i].ToString(), out p2));
} while(++i < n.Length && this.options.TryGetValue(n[i].ToString(CultureInfo.InvariantCulture), out p2));
}

// not a know option; either a value for a previous option
Expand Down
4 changes: 2 additions & 2 deletions src/mindtouch.dream/Aws/AwsS3Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public void PutFile(string path, AwsS3FileHandle fileHandle) {
var p = _rootPlug.AtPath(path);
if(fileHandle.TimeToLive.HasValue) {
var expiration = fileHandle.Expiration ?? DateTime.UtcNow.Add(fileHandle.TimeToLive.Value);
p = p.WithHeader(EXPIRE, expiration.ToEpoch().ToString())
.WithHeader(TTL, fileHandle.TimeToLive.Value.TotalSeconds.ToString());
p = p.WithHeader(EXPIRE, expiration.ToEpoch().ToInvariantString())
.WithHeader(TTL, fileHandle.TimeToLive.Value.TotalSeconds.ToInvariantString());
_expirationEntries.SetOrUpdate(path, expiration, fileHandle.TimeToLive.Value);
}
if(!string.IsNullOrEmpty(fileHandle.CacheControl)) {
Expand Down
6 changes: 3 additions & 3 deletions src/mindtouch.dream/Aws/AwsSqsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public Result<IEnumerable<AwsSqsMessage>> Receive(string queue, int maxMessages,
// CLEANUP: punting on attributes right now
var parameters = new Dictionary<string, string>();
if(maxMessages != AwsSqsDefaults.DEFAULT_MESSAGES) {
parameters.Add("MaxNumberOfMessages", maxMessages.ToString());
parameters.Add("MaxNumberOfMessages", maxMessages.ToInvariantString());
}
if(visibilityTimeout != AwsSqsDefaults.DEFAULT_VISIBILITY) {
parameters.Add("VisibilityTimeout", Math.Floor(visibilityTimeout.TotalSeconds).ToString());
parameters.Add("VisibilityTimeout", Math.Floor(visibilityTimeout.TotalSeconds).ToInvariantString());
}
return HandleResponse(false, queue, "ReceiveMessage", parameters, result,
m => AwsSqsMessage.FromSqsResponse(queue, m)
Expand All @@ -76,7 +76,7 @@ public Result<AwsSqsResponse> CreateQueue(string queue, TimeSpan defaultVisibili
{"QueueName", queue},
};
if(defaultVisibilityTimeout != AwsSqsDefaults.DEFAULT_VISIBILITY) {
parameters.Add("DefaultVisibilityTimeout", Math.Floor(defaultVisibilityTimeout.TotalSeconds).ToString());
parameters.Add("DefaultVisibilityTimeout", Math.Floor(defaultVisibilityTimeout.TotalSeconds).ToInvariantString());
}
return HandleResponse(true, null, "CreateQueue", parameters, result,
m => new AwsSqsResponse(m)
Expand Down
47 changes: 40 additions & 7 deletions src/mindtouch.dream/Xml/XDoc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ public XDoc Attr(string tag, XUri value) {
/// <param name="value">Value of the attribute</param>
/// <returns>Current XDoc instance</returns>
public XDoc Attr(string tag, int value) {
return Attr(tag, value.ToString());
return Attr(tag, value.ToInvariantString());
}

/// <summary>
Expand All @@ -1187,7 +1187,7 @@ public XDoc Attr(string tag, int value) {
/// <param name="value">Value of the attribute</param>
/// <returns>Current XDoc instance</returns>
public XDoc Attr(string tag, uint value) {
return Attr(tag, value.ToString());
return Attr(tag, value.ToInvariantString());
}

/// <summary>
Expand All @@ -1197,7 +1197,7 @@ public XDoc Attr(string tag, uint value) {
/// <param name="value">Value of the attribute</param>
/// <returns>Current XDoc instance</returns>
public XDoc Attr(string tag, long value) {
return Attr(tag, value.ToString());
return Attr(tag, value.ToInvariantString());
}

/// <summary>
Expand All @@ -1207,7 +1207,7 @@ public XDoc Attr(string tag, long value) {
/// <param name="value">Value of the attribute</param>
/// <returns>Current XDoc instance</returns>
public XDoc Attr(string tag, ulong value) {
return Attr(tag, value.ToString());
return Attr(tag, value.ToInvariantString());
}

/// <summary>
Expand Down Expand Up @@ -1523,6 +1523,9 @@ public XDoc Value(XUri value) {
/// <param name="value">Value to add</param>
/// <returns>Current XDoc instance</returns>
public XDoc Value(object value) {
if(value == null) {
return this;
}
if(value is DateTime) {
return Value((DateTime)value);
}
Expand All @@ -1538,10 +1541,40 @@ public XDoc Value(object value) {
if(value is bool) {
return Value((bool)value);
}
if(value != null) {
return Value(value.ToString());
if(value is sbyte) {
return Value(((sbyte)value).ToInvariantString());
}
return this;
if(value is byte) {
return Value(((byte)value).ToInvariantString());
}
if(value is short) {
return Value(((short)value).ToInvariantString());
}
if(value is ushort) {
return Value(((ushort)value).ToInvariantString());
}
if(value is int) {
return Value(((int)value).ToInvariantString());
}
if(value is uint) {
return Value(((uint)value).ToInvariantString());
}
if(value is long) {
return Value(((long)value).ToInvariantString());
}
if(value is ulong) {
return Value(((ulong)value).ToInvariantString());
}
if(value is float) {
return Value(((float)value).ToInvariantString());
}
if(value is double) {
return Value(((double)value).ToInvariantString());
}
if(value is decimal) {
return Value(((decimal)value).ToInvariantString());
}
return Value(value.ToString());
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/mindtouch.dream/dream/mimetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ public MimeType(string contentTypeWithParameters, Encoding charset, float qualit
_parameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
_parameters[PARAM_CHARSET] = charset.WebName;
_parameters[PARAM_QUALITY] = quality.ToString();
_parameters[PARAM_QUALITY] = quality.ToInvariantString();
_encoding = GetEncoding();
}

Expand All @@ -807,7 +807,7 @@ public MimeType(string contentTypeWithParameters, float quality) {
if(_parameters == null) {
_parameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
_parameters[PARAM_QUALITY] = quality.ToString();
_parameters[PARAM_QUALITY] = quality.ToInvariantString();
_encoding = GetEncoding();
}

Expand Down
2 changes: 1 addition & 1 deletion src/mindtouch.dream/dream/types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ private static void ParseFeatureSignature(XUri baseUri, string signature, out st
throw new ArgumentException("invalid feature signature", signature);
}
segments.Add(SysUtil.NameTable.Add(part));
names.Add(new KeyValuePair<int, string>(baseUri.Segments.Length + i, SysUtil.NameTable.Add(i.ToString())));
names.Add(new KeyValuePair<int, string>(baseUri.Segments.Length + i, SysUtil.NameTable.Add(i.ToInvariantString())));
} else if(part == "?") {

// we have an optional path (e.g. /?/)
Expand Down
4 changes: 2 additions & 2 deletions src/mindtouch.dream/phputils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private static void Serialize(string arg, Stream stream, Encoding encoding) {

private static void Serialize(long arg, Stream stream, Encoding encoding) {
stream.Write(encoding, "i:");
stream.Write(encoding, arg.ToString());
stream.Write(encoding, arg.ToInvariantString());
}

private static void Serialize(bool arg, Stream stream, Encoding encoding) {
Expand All @@ -191,7 +191,7 @@ private static void Serialize(bool arg, Stream stream, Encoding encoding) {

private static void Serialize(double arg, Stream stream, Encoding encoding) {
stream.Write(encoding, "d:");
stream.Write(encoding, arg.ToString());
stream.Write(encoding, arg.ToInvariantString());
}

private static void Serialize(System.Collections.IList list, Stream stream, Encoding encoding) {
Expand Down
4 changes: 2 additions & 2 deletions src/mindtouch.dream/plug/Plug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public Plug With(string key, bool value) {
/// <param name="value">Query value.</param>
/// <returns>New instance.</returns>
public Plug With(string key, int value) {
return new Plug(Uri.With(key, value.ToString()), Timeout, _headers, _preHandlers, _postHandlers, Credentials, _cookieJarOverride, MaxAutoRedirects);
return new Plug(Uri.With(key, value.ToInvariantString()), Timeout, _headers, _preHandlers, _postHandlers, Credentials, _cookieJarOverride, MaxAutoRedirects);
}

/// <summary>
Expand All @@ -449,7 +449,7 @@ public Plug With(string key, int value) {
/// <param name="value">Query value.</param>
/// <returns>New instance.</returns>
public Plug With(string key, long value) {
return new Plug(Uri.With(key, value.ToString()), Timeout, _headers, _preHandlers, _postHandlers, Credentials, _cookieJarOverride, MaxAutoRedirects);
return new Plug(Uri.With(key, value.ToInvariantString()), Timeout, _headers, _preHandlers, _postHandlers, Credentials, _cookieJarOverride, MaxAutoRedirects);
}

/// <summary>
Expand Down
49 changes: 47 additions & 2 deletions src/mindtouch.dream/system/stringutil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,10 @@ public static string DecodeHtmlEntities(this string text) {
if(v[0] == '#') {
if(char.ToLowerInvariant(v[1]) == 'x') {
string value = v.Substring(2);
return ((char)int.Parse(value, NumberStyles.HexNumber)).ToString();
return ((char)int.Parse(value, NumberStyles.HexNumber)).ToString(CultureInfo.InvariantCulture);
} else {
string value = v.Substring(1);
return ((char)int.Parse(value)).ToString();
return ((char)int.Parse(value)).ToString(CultureInfo.InvariantCulture);
}
} else {
string value;
Expand Down Expand Up @@ -1143,5 +1143,50 @@ public static string ToInvariantString(this double obj) {
public static string ToInvariantString(this float obj) {
return obj.ToString(CultureInfo.InvariantCulture);
}

/// <summary>
/// Call ToString() using InvariantCulture.
/// </summary>
/// <returns>A String that represents the sbyte value.</returns>
/// <param name="obj">float value</param>
public static string ToInvariantString(this sbyte obj) {
return obj.ToString(CultureInfo.InvariantCulture);
}

/// <summary>
/// Call ToString() using InvariantCulture.
/// </summary>
/// <returns>A String that represents the byte value.</returns>
/// <param name="obj">float value</param>
public static string ToInvariantString(this byte obj) {
return obj.ToString(CultureInfo.InvariantCulture);
}

/// <summary>
/// Call ToString() using InvariantCulture.
/// </summary>
/// <returns>A String that represents the short value.</returns>
/// <param name="obj">float value</param>
public static string ToInvariantString(this short obj) {
return obj.ToString(CultureInfo.InvariantCulture);
}

/// <summary>
/// Call ToString() using InvariantCulture.
/// </summary>
/// <returns>A String that represents the ushort value.</returns>
/// <param name="obj">float value</param>
public static string ToInvariantString(this ushort obj) {
return obj.ToString(CultureInfo.InvariantCulture);
}

/// <summary>
/// Call ToString() using InvariantCulture.
/// </summary>
/// <returns>A String that represents the decimal value.</returns>
/// <param name="obj">float value</param>
public static string ToInvariantString(this decimal obj) {
return obj.ToString(CultureInfo.InvariantCulture);
}
}
}
4 changes: 2 additions & 2 deletions src/tests/DreamMisc/Aws/AwsS3ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void Can_put_file_with_expiration() {
MockPlug.Setup(AwsTestHelpers.AWS.S3Uri)
.Verb("PUT")
.At(_config.RootedPath("foo", "bar"))
.WithHeader("x-amz-meta-ttl", 1.ToString())
.WithHeader("x-amz-meta-ttl", 1.ToInvariantString())
.WithBody(data)
.Returns(DreamMessage.Ok())
.ExpectAtLeastOneCall();
Expand Down Expand Up @@ -158,7 +158,7 @@ public void Read_file_with_lazy_expiration_returns_null() {
.At(_config.RootedPath("foo", "bar"))
.Returns(invocation => {
var msg = DreamMessage.Ok(data);
msg.Headers["x-amz-meta-expire"] = DateTime.UtcNow.Subtract(10.Minutes()).ToEpoch().ToString();
msg.Headers["x-amz-meta-expire"] = DateTime.UtcNow.Subtract(10.Minutes()).ToEpoch().ToInvariantString();
msg.Headers["x-amz-meta-ttl"] = "10";
return msg;
})
Expand Down
4 changes: 2 additions & 2 deletions src/tests/DreamMisc/Aws/MockMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ namespace MindTouch.Dream.Test.Aws {
public class MockMessage : AwsSqsMessage {
private static int NEXT;
public MockMessage() {
MessageId = (++NEXT).ToString();
MessageId = (++NEXT).ToInvariantString();
ReceiptHandle = Guid.NewGuid().ToString();
Body = new XDoc("doc").Elem("id", MessageId).Elem("receipt-handle", ReceiptHandle).ToCompactString();
}
public MockMessage(int id) {
MessageId = id.ToString();
MessageId = id.ToInvariantString();
ReceiptHandle = Guid.NewGuid().ToString();
Body = new XDoc("doc").Elem("id", MessageId).Elem("receipt-handle", ReceiptHandle).ToCompactString();
}
Expand Down
4 changes: 2 additions & 2 deletions src/tests/DreamMisc/PlugTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,15 +524,15 @@ public void AutoRedirect_only_follows_specified_times() {
mock.Service.CatchAllCallback = delegate(DreamContext context, DreamMessage request, Result<DreamMessage> response) {
totalCalls++;
_log.DebugFormat("call {0} to redirect", totalCalls);
response.Return(DreamMessage.Redirect(context.Uri.WithoutQuery().With("c", totalCalls.ToString())));
response.Return(DreamMessage.Redirect(context.Uri.WithoutQuery().With("c", totalCalls.ToInvariantString())));
};
var uri = mock.AtLocalMachine.At("redirect");
var redirects = 10;
var expectedCalls = redirects + 1;
var r = Plug.New(uri).WithAutoRedirects(10).Get(new Result<DreamMessage>()).Wait();
Assert.AreEqual(DreamStatus.Found, r.Status);
Assert.AreEqual(expectedCalls, totalCalls, "redirect without forward called incorrectly");
Assert.AreEqual(uri.With("c", expectedCalls.ToString()).ToString(), r.Headers.Location.ToString());
Assert.AreEqual(uri.With("c", expectedCalls.ToInvariantString()).ToString(), r.Headers.Location.ToString());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests/DreamMisc/ServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void Can_provide_list_of_args_as_repeated_params_to_feature() {
List<string> ids = new List<string>();
for(int i = 0; i < n; i++) {
p = p.With("id", i);
ids.Add(i.ToString());
ids.Add(i.ToInvariantString());
}
DreamMessage result = p.Get(new Result<DreamMessage>()).Wait();
Assert.IsTrue(result.IsSuccessful);
Expand Down
2 changes: 1 addition & 1 deletion src/tests/DreamMisc/XDoc-Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void XmlSerialization1() {

[Test]
public void ElementCount() {
Test("element count", _doc["bold"].ListLength.ToString(), "2");
Test("element count", _doc["bold"].ListLength.ToInvariantString(), "2");
}

[Test]
Expand Down
4 changes: 2 additions & 2 deletions src/tests/MindTouchTest.Sqs/Helpers/MockMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public class MockMessage {
//--- Constructors ---
public static SqsMessage NewMockMessage() {
var messageId = Guid.NewGuid().ToString();
var messageReceipt = (++NEXT).ToString();
var messageReceipt = (++NEXT).ToInvariantString();
return new SqsMessage(new SqsMessageId(messageId), new SqsMessageReceipt(messageReceipt),
new XDoc("doc").Elem("id", messageId).Elem("receipt-handle", messageReceipt).ToCompactString());
}

//--- Methods ---
public SqsMessage NewMockMessage(int id) {
var messageId = id.ToString();
var messageId = id.ToInvariantString();
var MessageReceipt = Guid.NewGuid().ToString();
var body = new XDoc("doc").Elem("id", messageId).Elem("receipt-handle", MessageReceipt).ToCompactString();
return new SqsMessage(new SqsMessageId(messageId), new SqsMessageReceipt(MessageReceipt), body);
Expand Down

0 comments on commit 6bd667e

Please sign in to comment.