1. 1

    Hoping to die.

    1. 5

      Hoping everything is working fine on your end. If not, please talk with your friend, families. Agreed weather, climate, current world is not really helping to make us feel better, but don’t let it grow on you. We gotta fight for better days.

      Please do take care!

      1. 1

        Thanks.

      2. 2

        Please take care of yourself. Sit on a beach, realize that politics is mostly sports but with legal systems, and the world is not in a very bad place. Life will go on, there are always more things to do and more opportunities to do them.

        1. 1

          Thank you.

      1. 3

        I’m creating a modern-looking 4chan-like website in mighty Rails. The reason is because my ISP has been blocked from 4chan at least a year ago, and I haven’t found 4chan alternatives that I like. I’m trying to make things as simple as possible. So simple in fact, everything under the root directory is a board, as in /*. And everything under /* is a post. Posts are stored linearly, but graphically grouped into a tree of posts and replies (which are also posts). Damn you ISP block! But also thank you ISP block.

        1. 1

          Ubuntu, Xfce4-session, Chromium, SoundCloud, Lobsters, YouTube, Geany, Vim, Xfce4-terminal, DuckDuckGo, Lua, Kazam (for screenshots)

          1. 7

            Writing up a blogpost on a conlang project I’ve been working on

            1. 2

              I’m also working on a conlang! What type of conlang it’s going to to be?

              1. 2

                This is part of a group effort on the conlang critic discord, but essentially we all worked together making a proto-language and then we’re gonna each evolve it to different things. I ended up doing a lot of the vocabulary building and fleshing it out into a (slightly) usable language. The vocabulary is very minimal at the moment (contains a superset of some common word lists we got from the duolingo forums), but it’s workable.

                I’ve also been working on morphology checking tools and (eventually) an auto-glosser at https://github.com/Xe/mapatei.

                My post is really gonna combine a lot of the notes together and make something close to a formal language description document.

                My plans to evolve the language are to create a more latin-like grammar where the root words are modified for case and allowing for free word order (instead of the VSO (verb/subject/object) word order we’re playing with for the proto-langauge).

            1. 1

              I’ll make a comment that will apply to your other gists posted on Lobsters as well, but code like this on its own is 100% sure to have bugs. I would suggest adding unit tests and possibly some CI around it and you’ll get more confidence in your projects so you can build something bigger!

              1. 1

                Can you please inform me about “CI”?

                1. 2

                  CI in this context refers to automatically running the unit tests every time a new commit is pushed.

                  There are a few companies offering this service for free to open source projects (eg travisCI, circleCI).

              1. 1

                I updated it so that you can focus only on the vector, and not worrying about its information (length, size).

                1. 4

                  Interesting. I needed a dynamic array library for a project of mine but I wanted it to be type-safe and generic. So I went with a giant macro that allows you to declare a vector for a type, giving you static methods for working with the vector of that type. Debugging when it’s broken can be difficult but you can find problems by bisect-deleting code. Additionally you cannot put comments within the macro which is… not great. But overall it’s still super useful and basically the only additional data structure I need in small projects. We’ll see how long it lasts.

                  1. 7

                    When I wrote C I used kvec (https://github.com/attractivechaos/klib/blob/master/kvec.h) which is quite small and easy to understand. Just wanted to point out another option.

                    1. 2

                      Thanks! I also wanted not to be worrying about capacity while using the library. Here’s an example (didn’t try to compile it):

                      #include <stdio.h>
                      
                      #include "slowjs/vector.h"
                      
                      int main() {
                        const char hello_world_s[] = "hello world!", hey_s[] = "hey";
                        vector_char hey = {0}, hello_world = {0};
                        vector_error err = E_VECTOR_OK;
                        int i = 0;
                      
                        // Copy from array example
                        err = vector_char_copy(&hello_world, hello_world_s, sizeof(hello_world_s));
                        if (err != E_VECTOR_OK) {
                          goto cleanup;
                        }
                        printf("%s\n", hello_world.elements);
                      
                        // Append to vector example
                        for (i = 0; i < sizeof(hey_s); i++) {
                          err = vector_char_push(&hey, hey_s[i]);
                          if (err != E_VECTOR_OK) {
                            goto cleanup;
                          }
                        }
                        printf("%s\n", hey.elements);
                      
                        return 0;
                      
                       cleanup:
                        vector_char_free(&hello_world);
                        vector_char_free(&hey);
                        return (int)err;
                      }
                      
                      1. 1

                        I like it.

                      2. 1

                        For such cases I would write a code generator that outputs the necessary .c/.h file(s) instead of macros

                        1. 1

                          Yeah. I’ve been thinking about it and probably should do it.

                      1. 4

                        Like many things in C, it is fairly easy to write an implementation of a dynamic array, but not necessarily trivial to handle the corner cases.

                        For instance, from a quick glance it appears none of the three examples linked here check if doubling the capacity will overflow. Two of them use an int to store the capacity, and overflowing that would be UB, and in all cases it could result in the allocated memory suddenly shrinking, introducing out of bounds memory accesses.

                        1. 1

                          Now, I changed that from “int” to “unsigned”.

                          1. 1

                            Honestly I posted mine because I am stuck on some memory bugs in my implementation right now and I was hoping someone would point something out. The issue I’m currently having has to do with invalid checksum for freed object:

                            slowjs(1679,0x106c0e5c0) malloc: Incorrect checksum for freed object 0x7faf7a403168: probably modified after being freed.
                            

                            While the error occurs in the vector library I get the feeling it is a bug in code using the vector not in the vector itself.

                            That said I totally forgot about dealing with using unsigned ints and having a max int check. Will fix.

                          1. 4

                            What inspired this? I often reach for bash’s printf when I need tricky output in a shell script. I’m interested in the motivation for the character delay feature in particular.

                            1. 0

                              I’ll tell you that like a story: A while ago, I tried to make a command-based game, and I thought of some genre for it, and it was sci-fi. I knew that in some movies and some games, terminals print text in green, character by character, with a short beep sound. Then I set up the main game functions (print, input, wait…), but then I thought “This game couldn’t be better than the 1 million ones out there, and no one likes old-school style anymore, plus, I wanted to make a 3D game in the first place, but I couldn’t, because of hardware limitations”. Then, I moved to try to make a command-based tool/useful program (because no matter what my current working field is, I always love creating things). I tried to get motivation for some time, then I thought “Why not give people a tool that prints like ‘echo’, but with green color, and character by character”. So I started working on that, I named it “echod” (echo deluxe) initially, but after many testings, I realized that ‘echod’ is slow compared to ‘echo’, and it also had more limitations on the argument number, so because I give up quickly and I’m lazy, I got angry and deleted the project directory (what a brilliant idea!). After some long time thinking of projects, implementing them and dismissing them, I returned back to ‘echod’, this time with more energy and another name that is shorter and not based on other names. So, I added more colors than green (using ANSI escape sequences), and made the delay variable, and added a timeout, (because when I was testing ‘put’, I realized it was exiting quickly after finishing writing), and the ability to print to stderr (because why not?). and escape sequences (based on ‘echo’, but with more), and text attributes (because ANSI escape sequences offered more than colors).

                              Did you like my story?

                            1. 16

                              The cat-v bunch will just love this ;-P

                              It’s like poking them in the eye with a stick.

                              1. 2

                                It just need a more complex build system like autoconf or cmake.

                                1. 2

                                  cmake really is the way to go to troll in this fashion. It forces an external dependency for building, whereas a release tarball could prebuild the autoconf/automake files.

                                2. -1

                                  ^w^

                                1. 2

                                  If this is for Linux, then I think sendfile() would be fastest, as it happens entirely within the kernel. If you can’t use sendfile() (old Linux kernel, non-Linux Unix system), then I think calling mmap() on the file being copied, then madvise(MADV_SEQUENTIAL) followed by write() would be a good thing.

                                  1. 1

                                    no need to write(), just mmap() the output file too and even use MADV_DONTNEED.

                                    1. 0

                                      Not Linux-specific

                                    1. 6
                                      char buf[src_size];
                                      

                                      wont this fail if source file is larger than RAM? I am guess more robust solutions (cp, rsync) dont have this issue.

                                      1. 3

                                        It should fail if it’s larger than the available stack size, I think.

                                        1. 1

                                          How about char* buf?

                                          1. 3

                                            I dont see that as helping the problem. Youd need a sliding window of a set size, say 1 GB that is emptied after that portion is copied - or you could just ignore the problem if its for personal use

                                            1. 1

                                              What do you mean a sliding window?

                                              1. 3
                                                1. 3

                                                  Its a buffer implementation - you would need to use something like this for a robust copy solution. if you dont care about supporting larger files you can ignore this

                                                  if you do care about supporting larger files - create buffer of say 1GB - load first 1GB of source file and copy to destination - rinse and repeat until file is copied - you might need to seek as well but I think not as I believe C read moves the cursor as well.

                                              2. 2

                                                You’ve changed the code now to just do:

                                                  char* buf;
                                                  fread(buf, 1, src_size, src);
                                                

                                                Won’t that just fail since buf is uninitialized?

                                                1. 0

                                                  I tested it, it didn’t

                                                  1. 4

                                                    You’re relying on undefined behavior then, which is inadvisable.

                                                    1. 1

                                                      Are you joking? Even the most cursory of checking triggers the warning:

                                                      $ x86_64-w64-mingw32-gcc  -Wall   copy.c
                                                      copy.c: In function ‘copy’:
                                                      copy.c:14:3: warning: ‘buf’ is used uninitialized in this function
                                                      [-Wuninitialized]
                                                      
                                                      1. 1

                                                        reply

                                                        I think OP is learning C.

                                              1. 5

                                                It’s the first time I see a #include inside a function body!? Any reason you did that? Are you looking for feedback on the code?

                                                1. 2

                                                  I made it a stand-alone function, kind of. I’m stupid. Just forget it

                                                1. 3

                                                  I don’t see the point… (pun intended :D) I’m actually serious, not enough examples, only a hello world I saw. Could you provide more examples? Classics like fibonacci, etc.

                                                  1. 3

                                                    Syntax here. I don’t have an interpreter handy, but I think you can print fibonacci numbers to infinity like so:

                                                    11[=>+=:]

                                                    Push 1, push 1, start a loop that duplicates, rotates to the back of the stack, adds the top two, prints it, repeat… 1 1, 1 1 1, 1 2, 1 2 2, 2 1 2, 2 3, 2 3 3, 3 2 3, 3 5...

                                                    1. 1

                                                      Kind of, don’t forget that the sequence starts at 0, and your program starts printing at 2. The real problem, is how to print the sequence up to a number.

                                                    2. 1

                                                      I added 2 examples, but still, I’m working on making an example about printing the fibonacci sequence up to number.

                                                      1. 2

                                                        You really need a way to define functions.

                                                        1. 1

                                                          I’m thinking on adding them in a compatible way.

                                                          1. 2

                                                            { begin parsing a definition, } end a definition and leave it on the stack.

                                                            any character with undefined body takes the function off the stack and so gives it a name.

                                                            {[=>+=:]}f to define f as that infinite fib

                                                            1. 1

                                                              But the problem is that the stack can contain only integers

                                                              1. 1

                                                                If you wanna add them, here’s the repository https://github.com/Unlimiter/point. Go wild.

                                                      1. 2

                                                        I like seeing new approaches to solving string parsing problems for C, C++ and Java. Some similar functions in the standard libraries tend to exhibit poor performance in larger, time-sensitive parsing problems (e.g., parse 100 million values in under 1s), and more alternatives are always appreciated. I’d recommend adding unit tests and a performance comparison vs similar functions from the string library. That is an interesting idea for finding string length, but since it relies on the math library, it might benefit from the fast math compiler flag. For me, the hook is always correctness and performance.

                                                        1. 4

                                                          This is not string parsing though, it takes an int, uses logarithms and floating point math to split out date components and sends it off to printf("%i...") (with a hardcoded ‘0’ rather than using %02i).

                                                          Not really sure of the problem being solved there, especially if the input is already an int and probably something upstream of it already did the string parsing?

                                                          For what its worth, I was able to replace it with:

                                                          $ cat moo.c
                                                          #include <stdio.h>
                                                          
                                                          int main() {
                                                            int date = 20190413;
                                                            printf("%04i-%02i-%02i\n", (int)(date / 10000), (int)((date / 100) % 100), (int)(date % 100));
                                                          }
                                                          

                                                          Maybe am missing something?

                                                          1. 1

                                                            I actually didn’t know about that syntax for fixed formats.

                                                          2. 1

                                                            Thanks! I agree.

                                                            1. 1

                                                              log10/exp10 produces the wrong result (for this purpose) for zero and any negative number.

                                                            1. 4

                                                              Why not just use the POSIX method l64a?

                                                              http://pubs.opengroup.org/onlinepubs/9699919799/functions/a64l.html

                                                              Or here is something similar with Python, uses 0-9A-Za-z-_. radix 64:

                                                              def strb(val, base=10):
                                                                 import string as cf
                                                                 dh = cf.digits + cf.ascii_uppercase + cf.ascii_lowercase + '-_'
                                                                 ko = ''
                                                                 while val:
                                                                    ko = dh[val % base] + ko
                                                                    val //= base
                                                                 return ko
                                                              

                                                              YouTube uses this for their video_ids.

                                                              1. 1

                                                                Because I didn’t search for similar methods.

                                                              1. 4

                                                                What’s main uses for this?

                                                                1. 4

                                                                  i don’t know. I just didn’t have anything better to do.

                                                                  1. 2

                                                                    Gotcha haha.

                                                                    1. 2

                                                                      That’s reason enough!

                                                                  1. 4

                                                                    What if you add a line to the beginning of the program, so that the old line 15 is now 16?

                                                                    A better question is, what problem are you trying to solve?

                                                                    1. 1

                                                                      This idea really just popped into my mind, that’s why I asked if it is useful in some way.

                                                                    1. 2
                                                                      bool notneg(int32_t n) {
                                                                          return (((uint32_t)n) / 0x80000000) == 0;
                                                                      }
                                                                      
                                                                      1. 1

                                                                        That uses a boolean technique (==).

                                                                        1. 2

                                                                          Fixed.

                                                                          #include <stdio.h>
                                                                          #include <stdbool.h>
                                                                          #include <stdint.h>
                                                                          
                                                                          bool notneg(int32_t n) {
                                                                              return 1 - (((uint32_t)n) / 0x80000000);
                                                                          }
                                                                          
                                                                          int main(void) {
                                                                              printf("notneg(0) = %d\n", notneg(0));
                                                                              printf("notneg(1) = %d\n", notneg(1));
                                                                              printf("notneg(500) = %d\n", notneg(500));
                                                                              printf("notneg(0x7FFFFFFF) = %d\n", notneg(0x7FFFFFFF));
                                                                              printf("notneg(-1) = %d\n", notneg(-1));
                                                                              printf("notneg(-500) = %d\n", notneg(-500));   
                                                                              printf("notneg(0x80000000) = %d\n", notneg(0x80000000));
                                                                              return 0;
                                                                          }
                                                                          
                                                                          1. 1

                                                                            Nice. But it uses advantage of int limit (I checked that 0x80000000 == INT_MIN). The main feature of my suggestion is that it should always be general, not just computer-based.

                                                                            1. 3

                                                                              All computers have finite memory, so a trick like this will always be available.

                                                                              Do you have a general solution that doesn’t require floating point numbers to solve integer inputs?

                                                                              1. 1

                                                                                Yes, I wrote a proper version of dbremner’s implementation.

                                                                                1. 3

                                                                                  No. Your code clearly uses floating point numbers to solve integer inputs (ie, 0.5 floats).

                                                                                  int abs(int i) {
                                                                                    return pow((i*i), 0.5);
                                                                                  }
                                                                                  

                                                                                  *edit: Could use something like this

                                                                                  #include <stdlib.h>
                                                                                  #include <stdio.h>
                                                                                  #include <stdint.h>
                                                                                  #include <stdbool.h>
                                                                                  
                                                                                  uint32_t
                                                                                  iroot(uint32_t n)
                                                                                  {
                                                                                      // UINT32_MAX = 2**32 - 1
                                                                                      for (uint32_t i = 0; i < 65536; ++i) {
                                                                                          if (n == i*i) {
                                                                                              return i;
                                                                                          }
                                                                                      }
                                                                                  
                                                                                      return -1;
                                                                                  }
                                                                                  
                                                                                  int
                                                                                  main(void)
                                                                                  {
                                                                                      printf("iroot(1) = %u\n", iroot(1));
                                                                                      printf("iroot(4) = %u\n", iroot(4));
                                                                                      printf("iroot(9) = %u\n", iroot(9));
                                                                                      printf("iroot(16) = %u\n", iroot(16));
                                                                                      printf("iroot(17) = %u\n", iroot(17));
                                                                                      printf("iroot(225) = %u\n", iroot(225));
                                                                                      printf("iroot(226) = %u\n", iroot(226));
                                                                                      return 0;
                                                                                  }
                                                                                  
                                                                                  1. 1

                                                                                    But where is the check for negative integers? iroot doesn’t conclude that n is negative or not.