Skip navigation
I made a string library from scratch in C.
0:00 / 30:18

I made a string library from scratch in C.

Valentin Ignatev

@valigo14K followers

Visit on X
483 likesDownloadOriginal post

23,836 views

I made a string library from scratch in C. Here's how and why Chapters: 00:00 - Intro 00:15 - Why 02:26 - First style - struct 05:16 - Second - Pascal style 06:50 - Comparing approaches 10:20 - single-header details 11:30 - Library implementation tour 15:05 - Splitting strings 23:50 - Looking at musl and glibc 27:13 - Wrapping up

33 Comments

Comments from the original post on X (Twitter)

  • Πλάτων's profile picture

    @platoff

    Вы молодец! Но интересно не это, а то что такой контент в текущее время безусловно для многих интересен, а для кого то вообще экзотика, хотя должно быть наоборот. Контент в котором детей учат считать и рассказывают про «счетные палочки» должен быть на детских каналах, а до взрослых каналов текущая «индустрия» вообще не доросла Вернее так: индустрия уже деградировала до того что смотреть про «счетные палочки» интересно и что-то «новое», дальше двинут совсем в идиотов которые даже на пальцах считать не умеют

    12 likes

  • Valentin Ignatev's profile picture

    @valigo

    Ну в целом да, это прям основа-основ, но очень много людей её скипают и кидаются сразу формошлёпить. Потом их заменяют ИИшкой, и некоторые, поняв, что произошло, возвращаются к основам. И тут появляюсь я )))

    7 likes

  • Πλάτων's profile picture

    @platoff

    Да, я и говорю что ты молодец. Чего не могу сказать про «многих» 🙂

    3 likes

  • William Schaub's profile picture

    @WilliamSchaub8

    I also believe Pascal got strings correct vs the null terminated nonsense. (it stores the length first and then the string follows)

    3 likes

  • OmittedVerstand's profile picture

    @OmittedVerstand

    A trick I also love is xor a ASCII character with space to make it from upper to lower or lower to upper case.

    3 likes

  • 🤫🔨 Геопоэтика тоски 🦄's profile picture

    @meowthsli

    У redis толковая реализация строк в составе, sds, пожалуй, одна из лучших. В любом случае, строки и без сборщика мусора это всегда больно

    3 likes

  • Valentin Ignatev's profile picture

    @valigo

    Да не, с аренками и стрингбилдерами норм

  • Mx_paia_19's profile picture

    @nao_mx_nao_20

    Yay ill have what to watch during lunch now

    2 likes

  • droug's profile picture

    @entropyreducers

    very pascal coded imo

    2 likes

  • Kknewkles Wildwision's profile picture

    @Kknewkles

    One time I needed to write a metaprogramming utility(we might've discussed this IRL :D) and at first I thought I'd write the v0 in Python, but then it occurred to me to just clone the exact usage I would want from Python -- and to my surprise, all I needed were 3 functions.

    2 likes

  • CaptainAmericaTex's profile picture

    @CaptAmericaTx

    Very nice, especially the int64_t size. In java (even 64 bit java) the native max string size is hold on a signed int32, meaning java code is stuck with max 2Gb strings, which is tiny for 2026 data needs. Your C code could inspire some Java folks to develop a big string library.

    1 like

  • chester's profile picture

    @mdsiaofficial

    Nice

    1 like

  • VicThor Dev's profile picture

    @VicThorDev6

    I started with Tsoding's view library about a year ago and added some utilities for parsing and it's been pretty great (I only wish there was something similar in stdlibc)

    1 like

  • Valentin Ignatev's profile picture

    @valigo

    I knew that Tsoding has his own string view library, but I intentionally avoided it so I don't spoil the implementation, which is the most fun part for me in this whole side quest :)

    3 likes

  • GrumpyBalkanSoftwareEngineer's profile picture

    @TheBalkanHacker

    What do you think number of string libraries in C is out there in total... back in the "prime" years of C, it seem like every one wrote their own personal stdlib extension lib. that they used for their projects 🤣 ... strings, dynamic arrays, what else was a must? ( In before Tsoding materializes out of nowhere ). Have to watch this after work!

    1 like

  • Netsu🇺🇦's profile picture

    @Pitometsu

    Emacs!

    1 like

  • Reece's profile picture

    @RLeRoux64

    Yay!

    1 like

  • SamProgramiz's profile picture

    @SamProgramiz

    The null terminator is just 1 byte, your approach seems unnecessary and overkill with no clear benefits

    1 like

  • Valentin Ignatev's profile picture

    @valigo

    You don't need to iterate over string every time you need to count it, and you can have many different views into the same character array - so allocation-free substrings

    4 likes

  • SamProgramiz's profile picture

    @SamProgramiz

    What about the additional memory needed to store the count?