May 25, 2021

On Tuesday, 25 May 2021 at 21:14:24 UTC, Vinod K Chandran wrote:

>

On Tuesday, 25 May 2021 at 00:41:50 UTC, zjh wrote:

>

switch to ie.
other websites is no problem.
I don't know why.

I too faced this problem in my laptop's chrome.

The js of this website must have problems.
I faced the problem too long.
other websites no problem.

May 26, 2021

On Tuesday, 25 May 2021 at 23:57:24 UTC, zjh wrote:

>

On Tuesday, 25 May 2021 at 21:14:24 UTC, Vinod K Chandran wrote:

>

On Tuesday, 25 May 2021 at 00:41:50 UTC, zjh wrote:

>

switch to ie.
other websites is no problem.
I don't know why.

I too faced this problem in my laptop's chrome.

The js of this website must have problems.
I faced the problem too long.
other websites no problem.

the website is ok, it's because, you and me in China mainland, and we just behind that wall, you just need a VPN proxy!

as in Chinese: 网站没问题,你需要梯子!

May 26, 2021

哦,原来如此.谢谢.

May 26, 2021
On Monday, 24 May 2021 at 14:02:14 UTC, Mike Parker wrote:
> The blog:
> https://dlang.org/blog/2021/05/24/interfacing-d-with-c-strings-part-one/
>


Good!

toStringz()

Technically we can use 'reserve()' for reserve memory. Possible, memory already reserved.
    s.reserve( s.length + 1 );
Then we can set trailing zero.
    s[ $ ] = '\0';
And return pointer.
    return s.ptr;
In this case we prevent memory allocation. Operations will be faster.

In other case we cam:
    auto copy = new char[s.length + 1];
    copy[0 .. s.length] = s[];
    copy[s.length] = 0;

    return &assumeUnique(copy)[0];

Example:
    immutable(char)* toStringz( ref string s )
    {
        if ( s.capacity <= s.length )
            s.reserve( s.length + 1 );

        char* cptr = cast( char* ) s.ptr; // C ptr
        char* zptr = cptr + s.length;     // zero ptr
        *zptr = '\0';

        return cast( immutable(char)* ) cptr;
    }

Test code: https://run.dlang.io/is/xZwwtw

May 26, 2021
On Wednesday, 26 May 2021 at 04:00:17 UTC, Виталий Фадеев wrote:
> On Monday, 24 May 2021 at 14:02:14 UTC, Mike Parker wrote:
>> The blog:
>> https://dlang.org/blog/2021/05/24/interfacing-d-with-c-strings-part-one/
>>
>
>
>
> Test code: https://run.dlang.io/is/xZwwtw

Example for using reserve.
Test code 2: https://run.dlang.io/is/aQsr8n
May 26, 2021
On Wednesday, 26 May 2021 at 04:27:16 UTC, Виталий Фадеев wrote:
> On Wednesday, 26 May 2021 at 04:00:17 UTC, Виталий Фадеев wrote:
>> On Monday, 24 May 2021 at 14:02:14 UTC, Mike Parker wrote:
>>> The blog:
>>> https://dlang.org/blog/2021/05/24/interfacing-d-with-c-strings-part-one/
>>>
>>
>>
>>
>> Test code: https://run.dlang.io/is/xZwwtw
>
> Example for using reserve.
> Test code 2: https://run.dlang.io/is/aQsr8n

Pull request to std.string: https://github.com/dlang/phobos/pull/8111
Review code, please.
May 26, 2021
On 5/25/21 9:00 PM, Виталий Фадеев wrote:

>      immutable(char)* toStringz( ref string s )
>      {
>          if ( s.capacity <= s.length )
>              s.reserve( s.length + 1 );
>
>          char* cptr = cast( char* ) s.ptr; // C ptr
>          char* zptr = cptr + s.length;     // zero ptr
>          *zptr = '\0';

That's undefined behavior because that location does not belong to the string. Here is an example that defeats the proposed toStringz:

void main()
{
    string s;
    s = "D string";

    auto c_string = toStringz( s );

    auto other = s;
    other ~= 'X';    // <-- Seemingly unrelated operation

// ...
}

puts accesses that unrelated 'X' and more bytes after that:

  C string: D stringX1^

Ali


May 27, 2021
On Wednesday, 26 May 2021 at 16:35:36 UTC, Ali Çehreli wrote:
> On 5/25/21 9:00 PM, Виталий Фадеев wrote:
>
> >      immutable(char)* toStringz( ref string s )
> >      {
> >          if ( s.capacity <= s.length )
> >              s.reserve( s.length + 1 );
> >
> >          char* cptr = cast( char* ) s.ptr; // C ptr
> >          char* zptr = cptr + s.length;     // zero ptr
> >          *zptr = '\0';
>
> That's undefined behavior because that location does not belong to the string. Here is an example that defeats the proposed toStringz:
>
> void main()
> {
>     string s;
>     s = "D string";
>
>     auto c_string = toStringz( s );
>
>     auto other = s;
>     other ~= 'X';    // <-- Seemingly unrelated operation
>
> // ...
> }
>
> puts accesses that unrelated 'X' and more bytes after that:
>
>   C string: D stringX1^
>
> Ali

Yes. True.

reserve/capacity - not for all cases.

May 27, 2021
On Thursday, 27 May 2021 at 03:40:02 UTC, Виталий Фадеев wrote:
> On Wednesday, 26 May 2021 at 16:35:36 UTC, Ali Çehreli wrote:
>> On 5/25/21 9:00 PM, Виталий Фадеев wrote:
>>
>> >      immutable(char)* toStringz( ref string s )
>> >      {
>> >          if ( s.capacity <= s.length )
>> >              s.reserve( s.length + 1 );
>> >
>> >          char* cptr = cast( char* ) s.ptr; // C ptr
>> >          char* zptr = cptr + s.length;     // zero ptr
>> >          *zptr = '\0';
>>
>> That's undefined behavior because that location does not belong to the string. Here is an example that defeats the proposed toStringz:
>>
>> void main()
>> {
>>     string s;
>>     s = "D string";
>>
>>     auto c_string = toStringz( s );
>>
>>     auto other = s;
>>     other ~= 'X';    // <-- Seemingly unrelated operation
>>
>> // ...
>> }
>>
>> puts accesses that unrelated 'X' and more bytes after that:
>>
>>   C string: D stringX1^
>>
>> Ali
>
> Yes. True.
>
> reserve/capacity - not for all cases.

Zero terminator not keeped after concatenate source string with other string.

auto dString = "D string" ~ 2.to!string;
auto cString = dString.toStringz();
dString = dString ~ "new tail";
// cString[ $-1 ] != '\'0';

May 27, 2021
On Thursday, 27 May 2021 at 03:52:32 UTC, Виталий Фадеев wrote:
> On Thursday, 27 May 2021 at 03:40:02 UTC, Виталий Фадеев wrote:
>> On Wednesday, 26 May 2021 at 16:35:36 UTC, Ali Çehreli wrote:
>>> On 5/25/21 9:00 PM, Виталий Фадеев wrote:
>>>
> // cString[ $-1 ] != '\'0';

// cString[ $ ] != '\'0';

1 2 3
Next ›   Last »