C++ Learning Community Forum
August 01, 2010, 03:25:06 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Stuck with boost::xpressives $1 and using it  (Read 778 times)
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1236



View Profile
« on: October 24, 2008, 04:42:15 AM »

What my code does is check its input for characters that should be escaped as hex characters in an output file. The problem comes with replacing the literal character with an escaped version, I passed $1 to a function but it was literally $1 instead of the characters I was hoping it would be. I'm trying to figure out how I can take the value my regex matched and replace the literal value with the escaped hex value. Any ideas?

Code
GeSHi (cpp):
#include <boost/xpressive/xpressive.hpp>
#include <iostream>
#include <sstream>
using namespace std;
using namespace boost::xpressive;
 
 
string bar(string foo)
{
stringstream sstream;
sstream<<"\\x"<<std::hex<<(int)foo[0];
return sstream.str();
}
 
int main()
{
    string indata="\x42\x01\x20\x21\x02\x03-";
    sregex escseq = sregex::compile("([^\\x20-\\x7D])");
    string::iterator it=indata.begin();
    cout<<regex_replace( indata, escseq, bar("$1") );
    cin.get();
 
    return 0;
}
Created by GeSHI 1.0.7.18
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
adeyblue
Dr. of C++ology
****
Posts: 653

Taming the turntables a beat at a time


View Profile WWW
« Reply #1 on: October 24, 2008, 05:26:14 PM »

You mean something like this?
Code
GeSHi (cpp):
// everything else as it was
cout << regex_replace( indata, escseq, "\\x$1");
 
Created by GeSHI 1.0.7.18

I'm not sure you can replace the matched char with it's ascii value just using strings. The xpressive string substitution page suggests that this would be more inline with what you want:

Code
GeSHi (cpp):
#include <boost/xpressive/regex_constants.hpp>
#include <iostream>
#include <sstream>
using namespace std;
using namespace boost::xpressive;
 
std::string escaper(const smatch &what)
{
   std::stringstream stream("\\x");
   stream << std::hex << (int)what[1].str()[0];
   return stream.str();
}
 
int main()
{
   string indata="\x42\x01\x20\x21\x02\x03-";
   sregex escseq = sregex::compile("([^\\x20-\\x7D])");
   cout << regex_replace( indata, escseq, escaper);
   cin.get();
   return 0;
}
 
Created by GeSHI 1.0.7.18
But I don't have 1.36 installed at the mo so I can't test it
Logged

ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1236



View Profile
« Reply #2 on: October 25, 2008, 05:00:13 AM »

Thanks, that helped a lot  Grin this is what i wound up with:
Code
GeSHi (cpp):
#include <sstream>
#include <iostream>
#include <boost/xpressive/xpressive.hpp>
using namespace boost;
using namespace xpressive;
 
 
 
std::string escaper(const smatch &what)
{
   std::stringstream stream;
   int charvalue=(int)what[1].str()[0];
    if(charvalue<=16)
    {
     stream <<"\\x"<<'0'<<std::hex << charvalue;
    }
    else
    {
     stream <<"\\x"<<std::hex << charvalue;
    }
   return stream.str();
}
 
int main()
{
   std::string indata="\x42\x01\x20\x21\x02\x03-";
   sregex escseq = sregex::compile("([^\\x20-\\x7D])");
   std::cout << regex_replace( indata, escseq, escaper);
   std::cin.get();
   return 0;
}
 
Created by GeSHI 1.0.7.18
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!