Skip to content

Commit

Permalink
remove uses of String.Format to avoid problems with curly braces (see…
Browse files Browse the repository at this point in the history
… issue migueldeicaza#1)
  • Loading branch information
rfranke committed Aug 17, 2014
1 parent 4302814 commit cbe74cd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
48 changes: 23 additions & 25 deletions redis-sharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,16 @@ void Connect ()

byte [] end_data = new byte [] { (byte) '\r', (byte) '\n' };

bool SendDataCommand (byte [] data, string cmd, params object [] args)
bool SendDataCommand (byte [] data, string cmd)
{
if (socket == null)
Connect ();
if (socket == null)
return false;

string s = args.Length > 0 ? String.Format (cmd, args) : cmd;
byte [] r = Encoding.UTF8.GetBytes (s);
byte [] r = Encoding.UTF8.GetBytes (cmd);
try {
Log ("S: " + String.Format (cmd, args));
Log ("S: " + cmd);
socket.Send (r);
if (data != null){
socket.Send (data);
Expand All @@ -259,17 +258,16 @@ bool SendDataCommand (byte [] data, string cmd, params object [] args)
return true;
}

bool SendCommand (string cmd, params object [] args)
bool SendCommand (string cmd)
{
if (socket == null)
Connect ();
if (socket == null)
return false;

string s = args != null && args.Length > 0 ? String.Format (cmd, args) : cmd;
byte [] r = Encoding.UTF8.GetBytes (s);
byte [] r = Encoding.UTF8.GetBytes (cmd);
try {
Log ("S: " + String.Format (cmd, args));
Log ("S: " + cmd);
socket.Send (r);
} catch (SocketException){
// timeout;
Expand All @@ -282,9 +280,9 @@ bool SendCommand (string cmd, params object [] args)
}

[Conditional ("DEBUG")]
void Log (string fmt, params object [] args)
void Log (string message)
{
Console.WriteLine("{0}", String.Format(fmt.Replace("\r\n", " "), args).Trim());
Console.WriteLine(message.Replace("\r\n", " "));
}

void ExpectSuccess ()
Expand All @@ -299,17 +297,17 @@ void ExpectSuccess ()
throw new ResponseException (s.StartsWith ("ERR ") ? s.Substring (4) : s);
}

void SendExpectSuccess (string cmd, params object [] args)
void SendExpectSuccess (string cmd)
{
if (!SendCommand (cmd, args))
if (!SendCommand (cmd))
throw new Exception ("Unable to connect");

ExpectSuccess ();
}

int SendDataExpectInt (byte[] data, string cmd, params object [] args)
int SendDataExpectInt (byte[] data, string cmd)
{
if (!SendDataCommand (data, cmd, args))
if (!SendDataCommand (data, cmd))
throw new Exception ("Unable to connect");

int c = bstream.ReadByte ();
Expand All @@ -328,9 +326,9 @@ int SendDataExpectInt (byte[] data, string cmd, params object [] args)
throw new ResponseException ("Unknown reply on integer request: " + c + s);
}

int SendExpectInt (string cmd, params object [] args)
int SendExpectInt (string cmd)
{
if (!SendCommand (cmd, args))
if (!SendCommand (cmd))
throw new Exception ("Unable to connect");

int c = bstream.ReadByte ();
Expand All @@ -349,9 +347,9 @@ int SendExpectInt (string cmd, params object [] args)
throw new ResponseException ("Unknown reply on integer request: " + c + s);
}

string SendExpectString (string cmd, params object [] args)
string SendExpectString (string cmd)
{
if (!SendCommand (cmd, args))
if (!SendCommand (cmd))
throw new Exception ("Unable to connect");

int c = bstream.ReadByte ();
Expand All @@ -371,17 +369,17 @@ string SendExpectString (string cmd, params object [] args)
//
// This one does not throw errors
//
string SendGetString (string cmd, params object [] args)
string SendGetString (string cmd)
{
if (!SendCommand (cmd, args))
if (!SendCommand (cmd))
throw new Exception ("Unable to connect");

return ReadLine ();
}

byte [] SendExpectData (byte[] data, string cmd, params object [] args)
byte [] SendExpectData (byte[] data, string cmd)
{
if (!SendDataCommand (data, cmd, args))
if (!SendDataCommand (data, cmd))
throw new Exception ("Unable to connect");

return ReadData ();
Expand All @@ -390,7 +388,7 @@ byte [] SendExpectData (byte[] data, string cmd, params object [] args)
byte [] ReadData ()
{
string r = ReadLine ();
Log ("R: {0}", r);
Log ("R: " + r);
if (r.Length == 0)
throw new ResponseException ("Zero length respose");

Expand Down Expand Up @@ -619,9 +617,9 @@ public byte [][] MGet (params string [] keys)
}


public byte[][] SendDataCommandExpectMultiBulkReply(byte[] data, string command, params object[] args)
public byte[][] SendDataCommandExpectMultiBulkReply(byte[] data, string command)
{
if (!SendDataCommand(data, command, args))
if (!SendDataCommand(data, command))
throw new Exception("Unable to connect");
int c = bstream.ReadByte();
if (c == -1)
Expand Down
8 changes: 4 additions & 4 deletions test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ static void Main (string[] args)
assert ((s = Encoding.UTF8.GetString (arr [2])) == "bär foo",
"expected \"foo bär\" to be \"bär foo\", got \"{0}\"", s);

r ["one"] = "world";
assert (r.GetSet ("one", "newvalue") == "world", "GetSet failed");
assert (r.Rename ("one", "two"), "failed to rename");
assert (!r.Rename ("one", "one"), "should have sent an error on rename");
r ["{one}"] = "world";
assert (r.GetSet ("{one}", "newvalue") == "world", "GetSet failed");
assert (r.Rename ("{one}", "two"), "failed to rename");
assert (!r.Rename ("{one}", "{one}"), "should have sent an error on rename");
r.Db = 10;
r.Set ("foo", "diez");
assert ((s = r.GetString ("foo")) == "diez", "got {0}", s);
Expand Down

0 comments on commit cbe74cd

Please sign in to comment.