Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A Better Way?

Reply
Thread Tools

A Better Way?

 
 
Luca Risolia
Guest
Posts: n/a
 
      03-10-2013
On 10/03/2013 09:57, Christian Gollwitzer wrote:
> Am 10.03.13 01:48, schrieb Luca Risolia:
>> On 10/03/2013 00:57, Juha Nieminen wrote:
>>> Luca Risolia <(E-Mail Removed)> wrote:
>>>> On 08/03/2013 22:21, Mike Copeland wrote:
>>>>> Is there a better way (less code, less convoluted, etc.) to
>>>>> access
>>>>> the last character of a string variable than this?:
>>>>>
>>>>> if(str2.at(str2.length()-1) != '/') str2 +="/";
>>>>
>>>> str2 = std::regex_replace(str2, std::regex("[^/]$|^$"), "$&/");
>>>
>>> Is this an attempt to come up with the most convoluted and least
>>> efficient solution to the problem as you can?

>>
>> It's a great benefit to get familiar with regular expressions (which are
>> not specific to C++ only) and profit by std::regex to solve those kind
>> of problems with strings, starting from simple cases like the above. By
>> looking at the code given by the OP and considering what "/" separators
>> could be for, I don't think efficiency is the main concern there, nor
>> the OP talked about efficiency explicitly.
>>

>
> I agree regexes are strong tools and love to see them in the standard
> library. However, Juha is right that the regex you give is quite
> advanced and hard to understand.


>An approach more similar to the OPs
> program looks like this:
>
> str2 = std::regex_replace(str2, std::regex("/?$"), "/");
>
> This RE matches the empty string at the end, when no / is present.


No, sorry, that cannot be "more similar" to what the OP asked (except
when the string is empty, as you said), because it adds an unwanted "/"
when str2 has a final "/".


 
Reply With Quote
 
 
 
 
Luca Risolia
Guest
Posts: n/a
 
      03-10-2013
On 10/03/2013 12:56, Nobody wrote:
> On Sun, 10 Mar 2013 01:48:06 +0100, Luca Risolia wrote:
>
>>>>> if(str2.at(str2.length()-1) != '/') str2 +="/";
>>>>
>>>> str2 = std::regex_replace(str2, std::regex("[^/]$|^$"), "$&/");
>>>
>>> Is this an attempt to come up with the most convoluted and least
>>> efficient solution to the problem as you can?

>>
>> It's a great benefit to get familiar with regular expressions (which are
>> not specific to C++ only) and profit by std::regex to solve those kind
>> of problems with strings, starting from simple cases like the above. By
>> looking at the code given by the OP and considering what "/" separators
>> could be for, I don't think efficiency is the main concern there, nor
>> the OP talked about efficiency explicitly.

>
> But if you're going to learn to use regexps, you should at least learn to
> use them correctly. In particular, you shouldn't construct multiple
> temporary regexp objects from a given string literal, as constructing a
> regexp can be expensive.


Ok...BUT I did not construct *multiple* temporaries There's *one*
temporary in the answer I gave to the OP. Again, simple question, simple
answer, that's all.

 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      03-10-2013
On Sun, 10 Mar 2013 14:46:10 +0100, Luca Risolia wrote:

>> But if you're going to learn to use regexps, you should at least learn
>> to use them correctly. In particular, you shouldn't construct multiple
>> temporary regexp objects from a given string literal, as constructing a
>> regexp can be expensive.

>
> Ok...BUT I did not construct *multiple* temporaries There's *one*
> temporary in the answer I gave to the OP.


Ah, but how many times does that code get executed?

Ultimately, your example isn't really any worse than those on
cplusplus.com or cppreference.com, both of which create instances of
std::regex as automatic variables within main().

But in "real" code, a given regexp is often matched against more than one
string. In that situation, not compiling the regexp each time is quite
important.

 
Reply With Quote
 
Luca Risolia
Guest
Posts: n/a
 
      03-10-2013
On 10/03/2013 21:36, Christian Gollwitzer wrote:
> Am 10.03.13 14:11, schrieb Luca Risolia:
>> On 10/03/2013 09:57, Christian Gollwitzer wrote:
>>> An approach more similar to the OPs
>>> program looks like this:
>>>
>>> str2 = std::regex_replace(str2, std::regex("/?$"), "/");
>>>
>>> This RE matches the empty string at the end, when no / is present.

>>
>> No, sorry, that cannot be "more similar" to what the OP asked (except
>> when the string is empty, as you said), because it adds an unwanted "/"
>> when str2 has a final "/".
>>

>
> Have you tried it?


Yes.

> Apparently I can't get it to work at all with my
> compiler I have only tried with a different tool (Tcl), and there it
> does what the OP wanted, as far as I understood the request:


> (Tests) 51 % regsub "/?$" "test/" "/"
> test/


Try your code with boost::regex with the ECMAScript grammar enabled,
which is the default in the standard: "test/" will be modified in "test//".

 
Reply With Quote
 
Luca Risolia
Guest
Posts: n/a
 
      03-11-2013
On 10/03/2013 23:05, Nobody wrote:
> On Sun, 10 Mar 2013 14:46:10 +0100, Luca Risolia wrote:
>
>>> But if you're going to learn to use regexps, you should at least
>>> learn to use them correctly. In particular, you shouldn't
>>> construct multiple temporary regexp objects from a given string
>>> literal, as constructing a regexp can be expensive.

>>
>> Ok...BUT I did not construct *multiple* temporaries There's
>> *one* temporary in the answer I gave to the OP.

>
> Ah, but how many times does that code get executed?


Only the OP knows how many times the code get executed in the program.
She might well notice that the compiler is able to move the
invariant constructor outside a loop, for example, and prefer to stay
with the one-line solution.

> Ultimately, your example isn't really any worse than those on
> cplusplus.com or cppreference.com, both of which create instances of
> std::regex as automatic variables within main().


...because they are examples aimed to showing what regex's are? Anyway,
that's off-topic.

> But in "real" code, a given regexp is often matched against more than
> one string. In that situation, not compiling the regexp each time is
> quite important.


Not relevant to the question, sorry.

 
Reply With Quote
 
Luca Risolia
Guest
Posts: n/a
 
      03-11-2013
On 11/03/2013 01:06, Juha Nieminen wrote:
> Luca Risolia <(E-Mail Removed)> wrote:
>>> Apparently I can't get it to work at all with my
>>> compiler I have only tried with a different tool (Tcl), and there it
>>> does what the OP wanted, as far as I understood the request:

>>
>>> (Tests) 51 % regsub "/?$" "test/" "/"
>>> test/

>>
>> Try your code with boost::regex with the ECMAScript grammar enabled,
>> which is the default in the standard: "test/" will be modified in "test//".

>
> I thought regexes always matched the largest possible string, rather
> than the shortest possible.


EMCAScript supports non-greedy matches.

> In other words, if your regex would be like "<.*>" and the input string
> is like "<p>hello</p>" then it would match the entire string (rather
> than just the "<p>" at the beginning.)


The correct regex in EMCAScript is "<.*>.*</.*>". To see how non-greedy
matches work consider another variation: "<(.*)>.*</\\1>", in this case
"\1" means that the trailing tag has to be the same as the leading tag.

> Likewise "/?$" would match the largest possible string, so in "test/"
> it would match the "/" at the end.
>
> If it instead matches the shortest possible string, I think that would
> break things badly (eg. suddenly ".*" would start to always matching a
> zero-length substring.)


If I am not wrong ".*" should match one empty (sub)string and the
non-empty string (two matches). Anyway, "everything" becomes "(.|\n)*"
in EMCAScript (note "\n", as the "." does not match a newline)

 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      03-11-2013
On Sunday, 10 March 2013 22:36:02 UTC+2, Christian Gollwitzer wrote:
> Am 10.03.13 14:11, schrieb Luca Risolia:
> > On 10/03/2013 09:57, Christian Gollwitzer wrote:
> >> An approach more similar to the OPs
> >> program looks like this:
> >>
> >> str2 = std::regex_replace(str2, std::regex("/?$"), "/");
> >>
> >> This RE matches the empty string at the end, when no / is present.

> >
> > No, sorry, that cannot be "more similar" to what the OP asked (except
> > when the string is empty, as you said), because it adds an unwanted "/"
> > when str2 has a final "/".

>
> Have you tried it? Apparently I can't get it to work at all with my
> compiler I have only tried with a different tool (Tcl), and there it
> does what the OP wanted, as far as I understood the request:
>
> (Tests) 50 % regsub "/?$" "test" "/"
> test/
> (Tests) 51 % regsub "/?$" "test/" "/"
> test/
> (Tests) 52 % regsub "/?$" "" "/"
> /


Btw ... we concentrate on empty string (that is uncertain how should
behave). OP's code was defective and description vague. The situation
is unclear. He was possibly asking for something else. Hard to tell.

For example similar questions arise when someone wants to extend
directory path with slash for further appending a file name.
If empty string means "current directory" (not "root directory")
then adding slash to it might result with wrong outcome.

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      03-11-2013
Luca Risolia <(E-Mail Removed)> writes:
>Only the OP knows how many times the code get executed in the program.


In programming, the number of time a piece of code gets
executed can be determined as late as at runtime.

Since the OP might not be present at every execution of his
programs (think of executions in the remote future) even the
OP himself cannot know in general how many times the code
gets executed during a specific execution or even what the
average of this number of times averaged over all executions
of the program is.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GL2 better than the XLs? Consumer grade HDs better than pro-sumer Mini DVs? dh@. DVD Video 1 08-28-2008 07:20 PM
The SCO case gets better and better.... thingy NZ Computing 2 12-10-2006 11:33 AM
Is splint really better than lint? Is there a better tool than splint? Peter Bencsik C Programming 2 09-21-2006 10:02 PM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM
Why doesn't the better camera have a better dpi? Tony Carlisle Digital Photography 6 10-04-2003 10:40 AM



Advertisments