nothrow function to tell if a string can be converted to a number?

Timothee Cour thelastmammoth at gmail.com
Sun Sep 8 20:29:39 PDT 2013


sorry I was a bit sloppy in my previous post. Here it is corrected:

 I was also curious whether there's functionality for the following (I
wrote my own versions but they might not consider all cases)

bool isStringLitteral(string); //tests whether a string represents a string
litteral
unittest{
  assert(`"abc"`. isStringLitteral);
  assert(`q{abc}`. isStringLitteral);
  assert(!"abc". isStringLitteral);
}

string escapeString(string); // escapes a string; analog to
std.process.escapeShellFileName which btw could be placed in a different
module than std.process (std.string?)
string escapeString(string a){
import std.array:replace;
return `r"`~a.replace(`"`,`" "\"" r"`)~`"`; // not sure if it'll always work
}
unittest{
  assert(`abc`. escapeString==r"`abc`");
}
string unEscapeString(string a); //inverse operation to escapeString
unittest{
  assert(`"abc"`. unEscapeString ==`abc`);
}

I several use cases for those, which I can explain if it's unclear.

On Sun, Sep 8, 2013 at 8:16 PM, Timothee Cour <thelastmammoth at gmail.com>wrote:

>
>
>
> On Fri, Sep 6, 2013 at 9:38 PM, Jonathan M Davis <jmdavisProg at gmx.com>wrote:
>
>> On Friday, September 06, 2013 21:15:44 Timothee Cour wrote:
>> > I'd like to have a function:
>> >
>> > @nothrow bool isNumberLitteral(string a);
>> > unittest{
>> > assert(isNumberLitteral("1.2"));
>> > assert(!isNumberLitteral("a1.2"));
>> > assert(!isNumberLitteral("a.b"));
>> > }
>> >
>> > I want it nothrow for efficiency (I'm using it intensively), and
>> try/catch
>> > as below has significant runtime overhead (esp when the exception is
>> > caught):
>>
>> You could try std.string.isNumeric.
>>
>
> beautiful, thanks. not clear if it's better in std.string rather than
> std.conv.
>
> While I'm at it, I was also curious whether there's functionality for the
> following (I wrote my own versions but they might not consider all cases) :
>

sorry I was a bit sloppy in my previous post. Here it is corrected:

unittest{
>   // isStringLitteral: tests whether a string represents a string litteral
>   assert(`"abc"`. isStringLitteral);
>   assert(`q{abc}`. isStringLitteral);
>   assert(!"abc". isStringLitteral);
> }
>



>
> //escapeString:
> /// escapes a to be pasted as is in D code (analog to
> std.process.escapeShellFileName which btw has little business to do in
> std.process)
> string escapeD(string a){
> import std.array:replace;
> return `r"`~a.replace(`"`,`" "\"" r"`)~`"`;
> }
>
> //inverse operation to unEscapeString
> string escapeString(string a);
>
>
> I have clear use cases for those, which I can explain if it's not obvious.
>





I meant s/isQuotedString/isStringLitteral



>
> unittest{
>   // isQuotedString: tests whether a string represents a quoted string.
>   assert(`"abc"`. isQuotedString);
>   assert(`q{abc}`. isQuotedString);
>   assert(!"abc". isQuotedString);
> }
>



>
> //escapeString:
> /// escapes a to be pasted as is in D code (analog to
> std.process.escapeShellFileName which btw has little business to do in
> std.process)
> string escapeD(string a){
> import std.array:replace;
> return `r"`~a.replace(`"`,`" "\"" r"`)~`"`;
> }
>
> //inverse operation to unEscapeString
> string escapeString(string a);
>
>
> I have clear use cases for those, which I can explain if it's not obvious.
>
>
>
> But it's true that it would be nice to have some sort of counterpart to
>> std.conv.to which checked whether a conversion was possible or which
>> returned
>> its argument via an out parameter and returned whether it succeeded or
>> not (or
>> something similar) for cases where you need to avoid throwing.
>>
>> http://d.puremagic.com/issues/show_bug.cgi?id=6840
>> http://d.puremagic.com/issues/show_bug.cgi?id=6843
>
>
> Agreed with this and std.conv.to calling a nothrow function that returns
> a boolean.
>
>
>>
>>
>> - Jonathan M Davis
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20130908/2ddc7198/attachment-0001.html>


More information about the Digitalmars-d-learn mailing list