#raw string literals?
1 messages · Page 1 of 1 (latest)
if you mean strings that don't do escape sequences for regex and stuff, then no
this is all there is
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
mb should have been more clear yeah raw string literal is this
this is a const byte slice?
well actually multiline string literals have no escapes apparently 🤔
(been a hot minute since i've done stuff in zig and i never did much anyways)
i literally just found this out but it's the first line in their doc lol https://ziglang.org/documentation/master/#Multiline-String-Literals
so you get raw like by accident with a multiline?
Escape secuences are for the compiler to not get confused
don't forget to put the semicolon
const text = \\bruh
;
aight i'm also def not familiar with the type system. you mentioned that i can coerce to a const byte slice but that implies that this isn't exactly a byte slice
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
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
Arrays are values in zig too, so it's not a ptr to a ptr
so ig how could i find out the length of a string literal
you use len, since arrays have a len property (which is comptime known)
and zig . operator deferences once if it's a ptr
is that like ```
const literal = "bruh";
len(literal)
literal.len
literal.len()```
ig which one of these
oh nice no arrow syntax ffs
.len
.len indeed
bet ty
oh cool what does that look like
?
like you did .*
Ah
can i put stuff instead of the *
.* is just a literal: Hey deference this ptr explcitly
ohhhhh ic its a deref kk
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
ic helps ppl not do like triple pointer or someth by accident
reference to the refernece to the whatever
yep
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
Since is a ptr to an array it has [] syntax
#include <stdio.h>
int main(void) {
const char * str = "bruh";
char c = *str;
return 0;
}
const bruh = "bruh";
pub fn main() void {
_ = bruh[0];
}
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?
Yup
oh nice that's handy
If you deference arrays you get the whole thing
Arrays are values
while in C arrays are pointers
is this just like in the reference doc btw
OHHH that just clicked
NICE
(ig for context i have a c and cpp background and yknow we've all had c/cpp moments that left a mark)
nice nice right on
well thanks a bunch this cleared up my question and i learned quite a bit
Arrays being values mean also that you can do
var some_array : [12:0]u8 = undefined;
some_array = "Hello World!".*;
OH NICE
Don't know if Hello World! is 12 lengthed but 
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
Slice Copying is that clown fiesta as you say lol
then use the .len thing
creativity is the limit =V
slice copying in zig? what does that look like
especially since len is comptime know, in all technicality you can use it to define the size of the array
three flavors for you to memcpy
do you literally call c memcpy or someth
lmao tyty
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;
lmao that is gnarly
Given string1 and string2 are not the same thing for the second
yep
There is always knowing how many bytes you will copy and abuse a few features to copy in array amounts syntax wise
yeye
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]
// 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].*
To be clear, that's 3 different ways to do it - not one big way. 😄
The insight here is that if you slice with a range who's start and end is compile-time-known, then you get a ptr-to-array, rather than a slice.
its my first and only language but its hard man
Start and end is comptime known*
You saw nothing.
oh cool that's slick
N and X are comptime or runtime know
k so mostly the typical slice syntax i've seen
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
lemme give a shot at english here:
starting at element x of string 1, M elements into string 2 but start copying into the Nth element of string 2
that's super handy actually
yeet edit error mb
np
but ye you got it
"Copy M elements from string1[X..] to string2[N..]."
yeah that's better lmoa
😄
like i said, english is hard man
Here to help 🤣
well thanks a ton for the help, imma close cause my question got answered
this warrants a full post dw
Told ya
but yeah i leanred a ton goddamn thanks
Glad we were able to help and teach
lmao you were right
take care, we'll probably talk soon cause my dumbass is def gonna be back
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
LMAO