Wednesday, October 10, 2012

Working with the console: Part 1

I spent some time trying to revise some of the code Fardad showed us in the edit() method. I decided that I would fiddle around with the switch case for left and right just for fun. While going through this editing, I went through a few iterations.
I started with this for the right arrow:

*curPosition += (*curPosition < fieldLength);

However, this did not work for shifting over the offset value. With that in mind, I attempted something along the lines of this:

*curPosition < fieldLength ? *curPosition++ : (*strOffset += (*strOffset < fieldLength));

Well, Let's just say this code did not work at all! Once it reached the end of the fieldLength the cursor would never move back. I'm sure it has something to do with it being an else case rather than an else if. Anyway, I don't think it would  be any faster than the regular format. I then dropped that idea all together and went with a more simple approach.  Here it is:

*curPosition += (*curPosition < fieldLength);
*strOffset += (*curPosition == fieldLength) && (*strOffset < fieldLength);

I might be able to refine it some more. Maybe even get it on one line, but for now I think it looks much more awesome.

No comments: