0

OK, I admit string manipulation has never been a strong point of mine when it comes to C++, and although i've had quite a success at various cases simply by using Boost, I'm still having some issues.

So, here's what I need (the example is precise)...


The input is :

position fen <arg_a1> <arg_a2> ... <arg_aN> moves <arg_b1> <arg_b2> ... <arg_bN>

(well, for those whose who are familiar with the topic, it's a UCI protocol's command)

All I want to do is :

  • get <arg_a1> <arg_a2> ... <arg_aN> (with the spaces included) into one string (that is the string between position and moves
  • also get <arg_b1> <arg_b2> ... <arg_bN> into another string (that is the string from moves till the end).

I've played a lot with Boost::split but obviously this is working much like strtok (accepting character and not string delimiters).

How should I go about that? What's the easiest way to solve this particular issue, without my reinventing the wheel (which I tend to do... quite often... lol)?

HINT : Obviously I can do it. But all I could think of right now is quite ugly. What I'm looking for is a C++-friendly way...

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • 3
    This seems pretty straightforward. How would you do it if you knew the exact location of the words `position` and `moves`? Now, how would you find the location of those words in the input string? I bet if you put those two things together, a possible solution will be clear. – Greg Hewgill Jan 21 '13 at 01:20
  • You might want to see this answer -- http://stackoverflow.com/a/236803/701092 – David G Jan 21 '13 at 01:23
  • 1
    +1 for Greg's comment. Further to that, you should do this using `std::string` functions - there's insignificant benefit from complicating this with boost split, regexps etc. – Tony Delroy Jan 21 '13 at 01:33
  • @GregHewgill Well, I definitely don't know why I had to read it by someone else, but guess what : It was solved by 2 simple `::find` and `::substr` statements. And for some weird reason, it doesn't strike me as ugly anymore... lol. Thanks, mate! ;-) – Dr.Kameleon Jan 21 '13 at 01:36
  • 1
    @Dr.Kameleon Next step: answer your own question below, then accept your answer as being correct. – Yakk - Adam Nevraumont Jan 21 '13 at 03:16
  • @Yakk Correct. Done. :-) – Dr.Kameleon Jan 21 '13 at 03:40

1 Answers1

0

And this the solution (with some numeric hacks) :

    // where are 'fen' and 'moves' within the initial string?
    int fenPos = input.find("fen");
    int movesPos = input.find("moves");

    // get the exact string but taking into account the position of our initial tokens
    // as well as their length (e.g. fenPos.length()+1 = 4, etc)
    string fen = input.substr(fenPos+4,movesPos-fenPos-5);
    string moves = input.substr(movesPos+6);

So for input :

"position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 moves f2f3 e7e6 g2g4", this returns :

fen   = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
moves = "f2f3 e7e6 g2g4"
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223