Jump to content


Photo

Challenge: Tag!


  • Please log in to reply
9 replies to this topic

#1 FLW_FTW

FLW_FTW

    HACK THE PLANET!

  • Members
  • 59 posts
  • Country:
  • Gender:Male
  • Location:San Francisco

Posted 28 June 2010 - 11:43 PM

So in a little project I'm working on, I found the need to be able to handle tags in a user inputed string. What I needed to do was parse a string for tags and translate them into ANSI escape sequences to emit the correct colors.
Ansi Escape Codes
Black: ESC [30m   -->Sets foreground color to black
Red: ESC [31m   -->Sets foreground color to red
Green: ESC [32m   -->Sets foreground color to green
Orange: ESC [33m   -->Sets foreground color to orange
Blue: ESC [34m   -->Sets foreground color to blue
Purple: ESC [35m   -->Sets foreground color to purple
Cyan: ESC [36m   -->Sets foreground color to cyan
White: ESC [37m   -->Sets foreground color to white

With tags of: "&x", "&r", "&g", "&o", "&b", "&p", "&c", and "&w", respectively.

Therefore, if a user typed in "&gHello!" the program would output "\033[0m\033[32mHello!". (\033 is escape, \033[0m is clear any previous color codes). Which would print "Hello!" in green.

My answer was this:
Spoiler


What is your answer?

Edited by FLW_FTW, 28 June 2010 - 11:46 PM.


#2 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 30 June 2010 - 09:55 PM

Neat.
Here's my sloppy crack at it...

Spoiler


EDIT:oops.
That first one is too litteral...

Here's one that actually changes the colors..
Spoiler

Edited by PurpleJesus, 30 June 2010 - 10:15 PM.

  • FLW_FTW likes this

#3 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 02 July 2010 - 08:35 PM

It was bothering me that I failed to actually return a string..

so here it is.. with my loopy style as usual...

Spoiler


edited to actually return a string too.. Couldn't make the compiler not throw warnings if I defined a temp buffer within the function though, so you still have to supply your source and working buffers.

Edited by PurpleJesus, 02 July 2010 - 09:42 PM.


#4 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 02 July 2010 - 08:47 PM

double post

Edited by PurpleJesus, 02 July 2010 - 09:39 PM.


#5 n3xg3n

n3xg3n

    "I Hack, therefore, I am"

  • Members
  • 960 posts
  • Country:
  • Gender:Male
  • Location:(703)

Posted 02 July 2010 - 11:43 PM

Here is my attempt, it works but I can tell that it is both sloppy and less than efficient. I am however so tired I can barely keep my eyes open let alone think properly (I've misspelled almost every word here, and if not for spell check it would look like I was drunk. That kind of bad tired =/)

I might revise later, until that time it at least works, and comments and criticisms are requested, but be kind =P

Spoiler

Edited by n3xg3n, 03 July 2010 - 08:51 AM.


#6 FLW_FTW

FLW_FTW

    HACK THE PLANET!

  • Members
  • 59 posts
  • Country:
  • Gender:Male
  • Location:San Francisco

Posted 06 July 2010 - 05:06 PM

Looks awesome, both of you.

n3xg3n, I don't know Ruby, so I can't really comment; PurpleJesus, very clean way of doing it, seems much simpler than my version.

Thanks for the input, both of you!

#7 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 06 July 2010 - 08:24 PM

Thank you FLW_FTW.


n3xg3n,
The part,
Spoiler

I'm guessing that's what scans the string looking for the '&' symbol.. care to elaborate on how that works? It seems to read backwards to my eye.. I did get it to run though. Nice.

Edited by PurpleJesus, 06 July 2010 - 08:24 PM.


#8 n3xg3n

n3xg3n

    "I Hack, therefore, I am"

  • Members
  • 960 posts
  • Country:
  • Gender:Male
  • Location:(703)

Posted 06 July 2010 - 10:45 PM

n3xg3n,
The part,

Spoiler

I'm guessing that's what scans the string looking for the '&' symbol.. care to elaborate on how that works? It seems to read backwards to my eye.. I did get it to run though. Nice.


We have a key-value hash (defined in TagColor.tagHash) which maps a string (the color letter) to a string (the control sequence).


I call the 'each' method for this hash, which accepts a block, so that block is run for each key-value pair, and each time through the value of the variables "key" and "value" represent a key-value pair from the hash.

In ruby, to use a variable in a string, you use #{variableName}, so the if the variable 'key' equals 'x', the string "&#{key}" is interpreted as "&x".

For each key-value pair in the hash, we call the gsub! (global substitute, and in ruby the "!" implies that the method is 'destructive' in that it edits the object in place) method on the string, which will search for "&#{key}" and replace it with the value variable.

Once this is all done, what happens is that we have a k-v hash, and for each key in that hash, the string is globally searched for `'&' + key` and replaces it with value.

Hope that explains it pretty well. ( 1337th overall post, w00t! =] )

#9 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 07 July 2010 - 12:54 PM


n3xg3n,
The part,

Spoiler

I'm guessing that's what scans the string looking for the '&' symbol.. care to elaborate on how that works? It seems to read backwards to my eye.. I did get it to run though. Nice.


We have a key-value hash (defined in TagColor.tagHash) which maps a string (the color letter) to a string (the control sequence).


I call the 'each' method for this hash, which accepts a block, so that block is run for each key-value pair, and each time through the value of the variables "key" and "value" represent a key-value pair from the hash.

In ruby, to use a variable in a string, you use #{variableName}, so the if the variable 'key' equals 'x', the string "&#{key}" is interpreted as "&x".

For each key-value pair in the hash, we call the gsub! (global substitute, and in ruby the "!" implies that the method is 'destructive' in that it edits the object in place) method on the string, which will search for "&#{key}" and replace it with the value variable.

Once this is all done, what happens is that we have a k-v hash, and for each key in that hash, the string is globally searched for `'&' + key` and replaces it with value.

Hope that explains it pretty well. ( 1337th overall post, w00t! =] )


That explains it pretty well.. Now I just have to read a book on Ruby so I know what to ask ;) I bet it's pretty simple after you play with it for a while though..
thanks.

#10 heisenbug

heisenbug

    Gibson Hacker

  • Members
  • 79 posts
  • Country:
  • Gender:Male

Posted 06 February 2011 - 03:05 AM

Perl with a regular expression.
Spoiler

Edited by heisenbug, 06 February 2011 - 03:06 AM.





BinRev is hosted by the great people at Lunarpages!