String.compareTo() question/issue?

Hi Spark buddies…

I’m writing a simple parser for a single character command syntax. Two of these single character commands are “&” and “%”. However, in my parser code, when I include:

if (cmd.compareTo("&")==0)
{

(where cmd is declared as “String cmd;”)

then the compiler seems to screw up somehow and the entire program starts behaving oddly. Take out that line and it’s all fine again. Change that line to something else such as:

if (cmd.compareTo(“D”)==0)
{

and everything works as it should. So I’m thinking of three possibilities here.

  1. The parser code is running in response to a cloud function call (i.e. it is exposed by spark.function…) and all web syntaxes make pretty liberal use of “&”, perhaps this has some bearing?

  2. Perhaps ampersand is a disallowed character in the string processing context I am using it? I know some languages and implementations use it as a continuation character across multiple physical lines.

  3. Perhaps there is a bug in the compareTo code, or the way it compiles?

Can anyone shine a light on this for me? It’s not an urgent problem, I CAN (and have) changed the design to use other characters, but it would be nice to understand why this happened.

Thanks as ever
Alan T

my curl doesn’t like an “&” terminating the command:

curl https://api.spark.io/v1/devices/55ff6e12345678936270287/myCommand -d access_token=0411d5bc0cc12345c89ceeb14c9d09fe9eee -d params=&

mac OSX yosemite

This is shell (commandline interpretter) getting in your way. Adding & at the end of command instructs shell to run in in background. Either backslash it, or put it in quotes (recommended):

curl https://api.spark.io/v1/devices/55ff6e12345678936270287/myCommand -d access_token=XXXX -d "params=&"

Also, & sign separates multiple values in query string, like this:

param1=value&param2=value

If you need to send “&” charracter, you need to urlencode it, which is %26. See this page for details: http://en.wikipedia.org/wiki/Percent-encoding

Also, you use -d 2 times (once for access token, then for other data, better to merge it into one parameter:

curl https://api.spark.io/v1/devices/55ff6e12345678936270287/myCommand -d "access_token=XXXX&params=%26"

Notice the usage of & to separate access token value and params value, then using urlencoded %26 as value.

(and don’t post your access_token, it is a key to your core)

1 Like

edited for security reasons :wink:

Hi guys, thanks for your responses. However, I don’t think I fully understand them.

This is not a run-time error that occurs when I execute this particular bit of code. When that line :

if (cmd.compareTo("&")==0)
{

is included in the program the program does all sorts of odd things before I even get to sending commands with curl.

Hi @AlanSparkMan

I think you should try turning the Spark preprocessor off (search the forum for the pragma) and include application.h.