Click here to Skip to main content
16,004,647 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Wordle 1,166 Pin
Cp-Coder28-Aug-24 0:10
Cp-Coder28-Aug-24 0:10 
GeneralRe: Wordle 1,166 - 2/6 Pin
ChandraRam28-Aug-24 0:58
ChandraRam28-Aug-24 0:58 
GeneralVisual Basic ain't THAT bad Pin
Dr. Plecostomus27-Aug-24 17:24
Dr. Plecostomus27-Aug-24 17:24 
GeneralRe: Visual Basic ain't THAT bad Pin
devenv.exe27-Aug-24 19:11
professionaldevenv.exe27-Aug-24 19:11 
GeneralRe: Visual Basic ain't THAT bad Pin
obermd28-Aug-24 4:26
obermd28-Aug-24 4:26 
GeneralRe: Visual Basic ain't THAT bad Pin
trønderen28-Aug-24 13:20
trønderen28-Aug-24 13:20 
GeneralRe: Visual Basic ain't THAT bad Pin
PIEBALDconsult30-Aug-24 15:26
mvePIEBALDconsult30-Aug-24 15:26 
GeneralY'all knew I was gonna talk about it. Pin
Jeremy Falcon27-Aug-24 14:23
professionalJeremy Falcon27-Aug-24 14:23 
So ya know... that Zig thing. As we all know, in a non-GC language, if you wanna return a non-literal string from a function that bad boy needs to go on the heap. And in the lovely land of C that means the caller is responsible for freeing it up yo. Now, Zig doesn't have a concept of a borrow checker like Rust, but what it does enforce is that you use allocators since there's no global one. These allocators keep track of what's going in and out of it. So, it's a tiny bit of overhead, but essentially you get a memory air traffic controller as the trade off. And if the air traffic doesn't like what you just did, it be like no bueno hoss.

BEHOLD! This use case. Zig doesn't have localization yet, so I have to interop with C. Ok, cool, which means I'm gonna wrap snprintf and return a string from that wrapper... on the heap. Dun dun dun. Plot thickens.
C
const c = @cImport({
    @cInclude("locale.h");
    @cInclude("stdio.h");
});

// ...

pub fn formatNumber(ally: std.mem.Allocator, number: u64) ![]u8 {
    // max is 18,446,744,073,709,551,615 (a 20 digit number with 6 commas)
    // but allow up to two chars per separator for anything non-standard
    var buff = [_]u8{0} ** 33; // 32 + 1 for terminator

    // %'8.2f for floats or lld for signed
    _ = c.setlocale(c.LC_NUMERIC, "");
    const n = c.snprintf(&buff, 33, "%'llu", number);

    const len: usize = @intCast(n);
    const result = buff[0..len];

    return try ally.dupe(u8, result);
}

Ok, C interop I already talked about. But, here's the cool part. Notice in the return I allocate on the heap with the passed in allocator (the idiomatic way so the caller can choose which allocator they want) by duplicating the stack buffer used during the C interop.

Cool cool cool, nothing out of the normal yet! Here's the cool part. In a unit test for it:
C
test "formatNumber should work with the max 64-bit unsigned integer size" {
    const ally = std.testing.allocator;
    const result = try formatNumber(ally, 18_446_744_073_709_551_615);
    defer ally.free(result);

    try std.testing.expect(std.mem.eql(u8, result, "18,446,744,073,709,551,615"));
}

Notice the defer line. If I forget it I literally get a compiler error because this particular allocator requires an explicit free (not all do). But, it's a compiler error if you forget it!

I mean, don't get me wrong, I'm sure a good linter can handle some of this. But, it's baked right into the compiler to say "nah bruh dat whack".

Edit: Now that I think about this I should make this routine generic. But, y'all get the idea.
Jeremy Falcon


modified 27-Aug-24 20:31pm.

GeneralRe: Y'all knew I was gonna talk about it. Pin
dandy7228-Aug-24 5:14
dandy7228-Aug-24 5:14 
GeneralRe: Y'all knew I was gonna talk about it. Pin
Jeremy Falcon28-Aug-24 5:42
professionalJeremy Falcon28-Aug-24 5:42 
QuestionAnyone watch the new Fall Guy movie? Pin
Jeremy Falcon27-Aug-24 12:32
professionalJeremy Falcon27-Aug-24 12:32 
GeneralBest VPN? Pin
Cp-Coder27-Aug-24 10:33
Cp-Coder27-Aug-24 10:33 
GeneralRe: Best VPN? Pin
Jwalant Natvarlal Soneji27-Aug-24 10:49
Jwalant Natvarlal Soneji27-Aug-24 10:49 
GeneralRe: Best VPN? Pin
0x01AA27-Aug-24 11:13
mve0x01AA27-Aug-24 11:13 
GeneralRe: Best VPN? Pin
k505427-Aug-24 12:01
mvek505427-Aug-24 12:01 
GeneralRe: Best VPN? Pin
Jeremy Falcon27-Aug-24 12:23
professionalJeremy Falcon27-Aug-24 12:23 
GeneralRe: Best VPN? Pin
theoldfool27-Aug-24 14:51
professionaltheoldfool27-Aug-24 14:51 
GeneralRe: Best VPN? Pin
  Forogar  27-Aug-24 15:53
professional  Forogar  27-Aug-24 15:53 
GeneralRe: Best VPN? Pin
Ravi Bhavnani27-Aug-24 21:36
professionalRavi Bhavnani27-Aug-24 21:36 
GeneralRe: Best VPN? Pin
Rage28-Aug-24 1:56
professionalRage28-Aug-24 1:56 
GeneralRe: Best VPN? Pin
Jeremy Falcon28-Aug-24 4:25
professionalJeremy Falcon28-Aug-24 4:25 
GeneralRe: Best VPN? Pin
dandy7228-Aug-24 5:11
dandy7228-Aug-24 5:11 
GeneralRe: Best VPN? Pin
Jeremy Falcon28-Aug-24 5:36
professionalJeremy Falcon28-Aug-24 5:36 
GeneralRe: Best VPN? Pin
dandy7229-Aug-24 3:20
dandy7229-Aug-24 3:20 
GeneralRe: Best VPN? Pin
Behzad Sedighzadeh29-Aug-24 5:31
Behzad Sedighzadeh29-Aug-24 5:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.