Re: string to char[4] FourCC conversion

2023-05-27 Thread kdevel via Digitalmars-d-learn
On Friday, 26 May 2023 at 13:18:15 UTC, Steven Schveighoffer 
wrote:

[...]
This worked for me:

```d
char[4] fourC(string s)
{
if(s.length >= 4)
return s[0 .. 4];


Silent truncation? Non-ASCII chars?


char[4] res = 0;


According to [1], [2] or [3] that should read

```
char[4] res = ' ';
```


res[0 .. s.length] = s;
return res;
}
```


[1] Multimedia Programming Interface and Data Specifications 1.0
IBM Corporation and Microsoft Corporation
August 1991, p. 11

https://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/riffmci.pdf


[2] http://ffmpeg.org/doxygen/trunk/raw_8c_source.html#l00050

[3] https://en.wikipedia.org/wiki/FourCC
"The byte sequence is usually restricted to ASCII printable 
characters, with space characters reserved for padding shorter 
sequences. [...] Some FourCCs however, do contain non-printable 
characters, and are not human-readable without special formatting 
for display; for example, 10bit Y'CbCr 4:2:2 video can have a 
FourCC of ('Y', '3', 10, 10) which ffmpeg displays as rawvideo 
(Y3[10] [10] / 0x0A0A3359), yuv422p10le."




Re: string to char[4] FourCC conversion

2023-05-26 Thread realhet via Digitalmars-d-learn
On Friday, 26 May 2023 at 13:18:15 UTC, Steven Schveighoffer 
wrote:



This worked for me:

```d
char[4] fourC(string s)
{
if(s.length >= 4)
return s[0 .. 4];
char[4] res = 0;
res[0 .. s.length] = s;
return res;
}
```
Sometimes I forget that the return does an implicit cast. And it 
looks inside the slice [..] if it constant or not. Lot of things 
going on under the hood...


Thank You!


Re: string to char[4] FourCC conversion

2023-05-26 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/26/23 8:19 AM, realhet wrote:

Hello,

Is there a way to do it nicer/better/faster/simpler?

```
char[4] fourC(string s)
{
 uint res;//Zero initialized, not 0xff initialized.
 auto cnt = min(s.length, 4),
     p = cast(char[4]*)();
 (*p)[0..cnt] = s[0..cnt];
 return *p;
}
```


This worked for me:

```d
char[4] fourC(string s)
{
if(s.length >= 4)
return s[0 .. 4];
char[4] res = 0;
res[0 .. s.length] = s;
return res;
}
```

Supporting returning as a 4-char array even when the input has less than 
4 makes it tricky. But not so bad.


-Steve