Solved

Parsing Escape Characters

Posted on 2004-09-17
5
871 Views
Last Modified: 2013-12-03
Greetings,
Lately I've taken interest in how to process escape characters internally for my own usage. For example, let's say I want "^" to be a special character that my script needs to escape. I would add the traditional escape character to it, so it becomes "\^". However, if that sequence was input, it would need to become "\\^". You get the idea.

Then beyond that, I need to understand the methodology to processing those escape sequences properly and recursively. Can somebody help me out with pseudo-code algorithms for doing this???

500 points for a complete, understandable walkthrough =)

Thanks
0
Comment
Question by:duerra
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:tzxie2000
Comment Utility
I'd like to say about C  escape characters '\'

if a string like :asdf\\\'\" the real mean is asdf\'" and \is the escape char,we can write

void Decode(char *src,char * des)
{
   char * p;
   p=src;
   q=des;
   while (*p!=0)
   {
       if(*p!='\\')
       {
           //copy char to des and move the point
           continue;
        }
       //get next char and copy to the des and move  the point
   }
}

a runable version about this should be warranted by the administrator about this is not a homework
other questions can be talk about
0
 
LVL 2

Accepted Solution

by:
MrRooster earned 500 total points
Comment Utility
I generally do something like (in vaguely C pseudocode):-

char c;

while (!eof()) {
       c = getNextChar();
       if (c=='\') {
              // It is an escape char, deal with it
              if (eof())
                  return endOfFileError;

              c = getNextChar();
              switch(c) {
                  case '\' :
                      handleNormalChar(c);
                      break;
                  case 'r' :
                       // code to deal with /r (an example)
                       break;
                   default :
                       // generic invalid char, or default char handler code
              }
       } else
            handleNormalChar(c);
}
0
 
LVL 4

Author Comment

by:duerra
Comment Utility
MrRooster, thanks for that little bit.  I will try it out and see if it works, and award as appropriate :).  It seems so simple, too - too bad I didn't figure that out myself.

tzxie2000, unfortunately, I was not able to understand what you are saying.  I can say that this isn't homework, by any means.  I have been wanting to develop some code at the office which required me to escape characters in JavaScript characters, but I was finding it rather cumbersome.  
0
 
LVL 2

Expert Comment

by:MrRooster
Comment Utility
If you're playing with javascript it does provide 'escape' and 'unescape' functions. (well, it does in a browser anyway, not 100% sure if they're part of the core spec but they might well be.), which are generally used for making strings URL and HTML safe, but it might be useful, try:-

alert( escape('hello world') )

and

alert( unescape('hello%20world'))
0
 
LVL 4

Author Comment

by:duerra
Comment Utility
Nah nah, this was/is something completely different.  Since the issue (which was a few weeks ago), I took the JS in a different direction, but I was still very curious as to how languages process escape sequences.  

Your code was valid.  Oddly enough, though, I worked on some code after I saw your post that does what I was looking to do, but it doesn't really resemble your code in any way.  I guess it just shows that there's more than one way to slaughter the beast.  The code I designed looks like this (PHP syntax):

$string = '~blah blah \~ blah \\\~blah blah';
$newStr = replace_unescaped('~', '.', $string);
echo "String: $string \n";
echo "New String: $newStr \n";

//Function assumes that the replacement string
//is used as an escape character/string
function replace_unescaped($strFind, $strReplacement, $strSubject)
{
      $offset = 0;
      while(($loc = strpos($strSubject, $strFind, $offset)) !== false)
      {
            $numSlashes = 0;
            
            $start = $loc-1; //Start one char before the location of $strFind
            //Double backslash below required here to not escape the last single quote
            while(($start-$numSlashes) >= 0 && substr($strSubject, $start-$numSlashes, 1) == '\\')
            {
                  $numSlashes++;
            }
            
            //If it's an even number of backslashes, then the backslash is
            //not escaped, and is our string -- replace
            if($numSlashes%2==0)
            {
                  $beg = substr($strSubject, 0, $loc);
                  $mid = $strReplacement;
                  $end = substr($strSubject, $loc+strlen($strFind));
                  $strSubject = $beg.$mid.$end;
                  $offset = $loc+strlen($strReplacement);
            }
            else
            {//The escape character has been escaped - ignore
                  $offset = $loc+strlen($strFind);      
            }
      }
      return $strSubject;
}

This outputs:
String: ~blah blah \~ blah \\~blah blah
New String: .blah blah \~ blah \\.blah blah
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now