How to use string::erase (it adds garbage) - c++

I hope someone is able to help me with this.
I have some code, where I have a string variable data. data contains always something like this: "'401454654". It is always a ' with a number in the back. I want to remove the ' in the front. It is also possible, that data is an empty string. My current solution looks like this:
string data = /* ... */;
if(!data.empty())
data.erase(data.begin());
else
cout << "Error in line ...." << endl;
The interesting thing is, that I always get the correct string with only the number, or an empty string. But sometimes I get some weird characters plus the original '401454654 back. I really do not know, what the cause of this is.
Tested on g++ 4.6 and g++ 4.9 linaro on both windows and linux. Always the exact same result. I hope someone can give me an advice.

Sorry for the late answer. I solved it by myself. I actually do not know the bug in the implementation, which I uploaded on gist, but I implemented it a second time in a far better way(For the first one I had only a few minutes time to implement it). Thanks for the help.

You might be seeing this bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60278
It's interesting though, I was able to get this to compile in gcc 4.8.1:
data.erase(data.begin());
If it is the bug you can just implement the code that it's doing under the hood:
copy(next(data.begin()), data.end(), data.begin());
data.pop_back();

Related

How to put format code the right way in Python?

I try to learn Python via LPTHW (Learn Python The Hard Way), on the ex05 (its about format code), the code that he give isn't working, I need to use different code to get the same result.
I already tried deleting the parentheses, give space between f and the double quote.
At last I use the % (%s and %d) not the f (f"bla bla {bla}") one
What LPTHW expect is the first code to give the same result as the second one, yet it give me invalid syntax. There's no way the computer wrong right?
So what is the problem? Because when I try to find this problem, no one have the same problem as me.
I'm sure I type it right, because after that I tried to copy the exact code from the page and it still not working.

Using comma vs underscore in urls

Only the first one works, second and third do not. Any ideas why?
http://abc.def.ghi.com:8080/mango?ll=76.93839283938492_-145.126282939231&locale=en_US&q= //returns json response
http://abc.def.ghi.com:8080/mango?ll=76.93839283938492,-145.126282939231&locale=en_US&q= //does not work, gives "Bad Request"
http://abc.def.ghi.com:8080/mango?ll=76.93839283938492%2C-145.126282939231&locale=en_US&q= //does not work, gives "Bad Request"
Okay so, here is my understanding. In general, comma's are not typically used in URL's, but it is possible to have them, just not commonly practiced. For your #2 and #3 lines, there are a couple of things that could be causing the bad requests to happen.
Here is a link worth looking into to get a better understanding. But from what I understood from it is that when you are using those links, its not encoding the comma's properly to allow you to use them in the URL address. So the interpreter could be stumbling over the addresses once it hits the comma "," because it doesn't know what to do with it.
" https://www.searchenginenews.com/sample/content/are-you-using-commas-in-your-urls-heres-what-you-need-to-know "
Hope this helps, I know my answer isn't much, but you brought up an interesting question! I'm curious to learn more about it too.

Trouble with C++ Regex POSIX character class

I'm trying to create a regex that is capable of analysing something like this:
002561-1415179671591i.jpg
The second part is a unix timestamp (before the i), and I need to extract that. I came up with the following syntax, but std::regex keeps throwing a regex_error before I even check for a match and I'm not too sure what's wrong.
Here's what I've got so far:([:d:])*-[[:d:]]*([:alpha:])\.jpg
The C++ code line that throws the error is the constructor to regex
std::regex reg(regex_expr);
where regex_expr is a string that has been read in from a file.
Really appreciate any help you can give.
Edit: I wrote a try catch section, and it seems I'm getting the following error code
std::regex_constants::error_brack
Edit 2: Ok... seems I'm still getting the error even with a cut-down test:
([:alpha:])*
Edit 3:
Seems I can't get any expression to work. Here's a bit more info. I'm using clang++ 3.5.0 on Kubuntu 14.04.
Not sure that [:d:] can stand for [:digit:]. [EDIT] (It seems it's possible)
When you use a POSIX character class, it must be enclosed in a character class like that:
[[:digit:]]
(This syntax allows to compose other classes [[:digit:]ab])
so:
std::string regex_expr = "([[:digit:]]*)-([[:digit:]]*)([[:alpha:]])\\.jpg";
or if you use the basic mode:
std::string regex_expr = "\\([[:digit:]]*\\)-\\([[:digit:]]*\\)\\([[:alpha:]]\\)\\.jpg";
I would rather use the perl-compatible syntax instead of character classes:
\d+-(\d+)[a-z]*\.jpg
After many tests, for whatever reason, I couldn't get this to work. As I had boost anyway, I tried out the boost::regex and it worked straight away, so I presume that something to do with either clang, or the version of the standard on this pc just wasn't working.
So simply put, tried boost and it worked straight away. Not much of an answer, but I guess that's how things go sometimes.

__FILE__ Returns a String with "\/" in the Path

I use the __FILE__ macro for error messages. However, sometimes the path comes back as E:\x\y\/z.ext. It does this for specific files.
For example, E:\programming\v2\wwwindowclass.h comes back as E:\programming\v2\/wwwindowclass.h and E:\programming\v2\test.cpp comes back as E:\programming\v2\test.cpp. In fact, the only file in the directory that works seems to be test.cpp.
To work around this, I used jmucchiello's answer to this question to replace any occurrence of "/" with "\". This worked fine, and the displayed path changed to a normal one.
The problem was when I tried it on Windows 7 (after using XP). The string came up as (null) after calling the function.
Along with this, I sometimes get some seemingly random error 2: File not found errors. I'm unsure of whether this is related at all, but if there's an explanation, it would be nice to hear.
I've tried to find why __FILE__ would be returning the wrong string, but to no avail. I'm using GNU g++ 4.6.1. I'm not actually sure yet if the paths that were wrong in XP were wrong in Windows 7 too. Any insight is appreciated.
The function in the linked question appears to return NULL if there are no changes to make. Probably Windows 7 doesn't suffer from the \/ problem (in some cases).
As per MSalters's comment:
Typically, the compiler does so when you pass #include "v2/wwwindowclass.h" to the compiler.
Since every file has its own include statements, you can (but shouldn't) mix the two styles.
This was the case. My compiler automatically adds a forward slash.

C++ Boost's sregex_token_iterator crash

I'm using the following code to get the image filenames from an HTML file.
The code goes somehow like this:
std::tr1::regex term=(std::tr1::regex)r;
const std::tr1::sregex_token_iterator end;
for (std::tr1::sregex_token_iterator i(s.begin(),s.end(), term); i != end; ++i)
{
std::cout << *i << std::endl;
}
s is a string that is already declared and contains the full string of the file.
r is a string that contains the regex term to look for.
This code does actually retrieve the values from the file correctly, but after reaching the last one it crashes. It might have to do with the token_iterator i, but I don't have a clue of what is causing it or how to fix it.
I don't know if you already solved the problem, but find my suggestions below:
Did you try to change the ++i to i++?
Did you look at the HTML file to see if the first filename that cout shows is in fact the first one in the file?
I think the first loop on cout will print the second match in the HTML file.
If you already solved it please let me know the code applied, I'm working with boost regex and it would help me on future problems that I may have.
Regards,
Tchesko.
I really forgot about this-- I'm pretty sure there was an external problem on this, linker-related, so it was kinda hard to figure out. But the code was fine.