#raw string literals?

1 messages · Page 1 of 1 (latest)

pure solar
#

just wondering if zig's got raw string literals. btw if there's a better way/place to ask lmk cause idk if this warrants an entire help post

wild ruin
#

this warrants a full post dw

#

what is a raw string literal for you

candid marsh
#

if you mean strings that don't do escape sequences for regex and stuff, then no

#

this is all there is

wild ruin
#

String literals are just *const [N:0]u8

#

A pointer to a constant array of bytes

#

that's it

#

it's as raw as it gets

pure solar
pure solar
wild ruin
#

You can coerce it into a []const u8

#

indeed

candid marsh
#

well actually multiline string literals have no escapes apparently 🤔

pure solar
#

(been a hot minute since i've done stuff in zig and i never did much anyways)

candid marsh
pure solar
#

so you get raw like by accident with a multiline?

wild ruin
#

Escape secuences are for the compiler to not get confused

pure solar
#

wait so i can just use that multiline syntax wiht one line ig

#

that'll work

hardy idol
#

don't forget to put the semicolon

wild ruin
#

Well two lines

#

one for the string, one for the semicolon

hardy idol
#
const text = \\bruh
;
pure solar
#

yeye tyty

#

yeah that works just fine

pure solar
wild ruin
#

Well, it is, just that they mean two different things in reality:
a slice in zig is a multi item ptr and a usize length
a string literal is a ptr to an array

pure solar
#

icic so its a representation thing

#

slices are represented with an extra word for a length

#

and the string literal is an array but you just have the pointer

wild ruin
#

Arrays are values in zig too, so it's not a ptr to a ptr

pure solar
#

so ig how could i find out the length of a string literal

wild ruin
#

you use len, since arrays have a len property (which is comptime known)

#

and zig . operator deferences once if it's a ptr

pure solar
#

is that like ```
const literal = "bruh";

len(literal)

literal.len

literal.len()```

ig which one of these

pure solar
hardy idol
#

.len

wild ruin
#

.len indeed

pure solar
#

bet ty

wild ruin
#

you can also copy the array into the stack

#

so you can edit the literal

pure solar
#

oh cool what does that look like

wild ruin
#
const literal = "bruh";
var copy = literal.*;
#

or shorter:

var copy = "bruh".* ;
pure solar
#

oh cool

#

what else can i do with that . there?

#

can i take slices?

wild ruin
#

?

pure solar
#

like you did .*

wild ruin
#

Ah

pure solar
#

can i put stuff instead of the *

wild ruin
#

.* is just a literal: Hey deference this ptr explcitly

pure solar
#

ohhhhh ic its a deref kk

wild ruin
#

zig . will deference only once

#

so it can deal with one layer of indirection

#

why not more, because it's more explicit where a ton of indirection is happening

#

so it's less likely someone writes big indirection code

pure solar
#

ic helps ppl not do like triple pointer or someth by accident

#

reference to the refernece to the whatever

wild ruin
#

yep

pure solar
#

icic

#

one other thing, in c when you deref a string literal you just get the first element and then you can use pointer arithmetic to change what element you get by the deref but here you deref the literal and it gets you the entire array

#

what's the rules behind that

wild ruin
#

Since is a ptr to an array it has [] syntax

pure solar
#
#include <stdio.h>

int main(void) {
    const char * str = "bruh";
    char c = *str;

    return 0;
}
wild ruin
#
const bruh = "bruh";

pub fn main() void {
   _ = bruh[0];
}
pure solar
#

kkk

#

idk if you can actually subscript string literals in c honestly

#

but ig my question is when you dereference arrays in zig does it return the whole array?

wild ruin
#

Yup

pure solar
#

oh nice that's handy

wild ruin
#

If you deference arrays you get the whole thing

#

Arrays are values

#

while in C arrays are pointers

pure solar
#

is this just like in the reference doc btw

pure solar
#

NICE

#

(ig for context i have a c and cpp background and yknow we've all had c/cpp moments that left a mark)

wild ruin
#

yup

#

C/CPP first languages here

#

lol

pure solar
#

nice nice right on

#

well thanks a bunch this cleared up my question and i learned quite a bit

wild ruin
#

Arrays being values mean also that you can do

var some_array : [12:0]u8 = undefined;
some_array = "Hello World!".*;
pure solar
#

OH NICE

wild ruin
#

Don't know if Hello World! is 12 lengthed but shruggin

pure solar
#

aight so array copying isn't an idiot clown fiesta with like sprintf or someth

#

lmao you can set hello world to a var earlier

wild ruin
#

Slice Copying is that clown fiesta as you say lol

pure solar
#

then use the .len thing

wild ruin
pure solar
wild ruin
#

especially since len is comptime know, in all technicality you can use it to define the size of the array

wild ruin
#

lol

pure solar
#

@candid marsh thanks a bunch for your help too btw

#

welp

wild ruin
#

three flavors for you to memcpy

pure solar
#

do you literally call c memcpy or someth

wild ruin
#

let me write this down, I hate doing memcpy, give me a sec

#

lol

pure solar
#

lmao tyty

wild ruin
#

there is:

// string1 and string 2 are two []u8 
std.mem.copy(u8,string2, string1);

@memcpy(string2.ptr, string1.ptr, string1.len);

for (string1) |s, i|
   string2[i] = s;
pure solar
#

lmao that is gnarly

wild ruin
#

Given string1 and string2 are not the same thing for the second

pure solar
#

yeye

#

and that's dest then source right

wild ruin
#

yep

pure solar
#

i don't remember the c convention off the top of my head

#

kkk tyty

wild ruin
#

There is always knowing how many bytes you will copy and abuse a few features to copy in array amounts syntax wise

pure solar
#

yeye

winter thunder
# pure solar can i take slices?

You can, but without the . - arrays and slices both have indexing syntax: x[i].
And you can index with a range to get a slice: x[a..b]

wild ruin
#
// N is the start of the string destination  
// M is a set amount of elements
// X is the start of the string 1 to copy
string2[N..][0..M].* = string1[X..][0..M].*
winter thunder
pure solar
#

ikik

#

sorry english moment

winter thunder
pure solar
#

its my first and only language but its hard man

winter thunder
wild ruin
#

N and X are comptime or runtime know

pure solar
wild ruin
#

just stating that you start from that element

#

the M is needed to be comptime known

#

since when is comptime known the compiler interprets it as: *[M]T
T being the type ofc

pure solar
#

that's super handy actually

wild ruin
#

yup

#

you skipped a copy in after the first comma

#

But I know what you mean

#

lol

pure solar
#

yeet edit error mb

wild ruin
#

np

pure solar
#

but ye you got it

winter thunder
pure solar
#

yeah that's better lmoa

winter thunder
#

😄

pure solar
#

like i said, english is hard man

winter thunder
#

Here to help 🤣

pure solar
#

well thanks a ton for the help, imma close cause my question got answered

wild ruin
#

this warrants a full post dw
Told ya

pure solar
#

but yeah i leanred a ton goddamn thanks

wild ruin
#

Glad we were able to help and teach

pure solar
#

lmao you were right

#

take care, we'll probably talk soon cause my dumbass is def gonna be back

wild ruin
#

and we will be here cuz, we are getting paid in exposure (?)
Nah, being serious we will be here for the sake of helping

#

glgl

pure solar
#

LMAO