I've recently done some touchups to my SubStrings library, and have reminded myself of my own undying glory, er, I mean, come to the conclusion that it's useful enough to warrant me advertising it a little bit. I use it in a large percentage of my projects nowadays. I thought I'd showcase part of what makes it awesome to me.
Before we begin, there is one thing I must mention: SubStrings is only designed to work well with null terminated strings only. It's almost certainly unsafe to use it for anything else.
And, one last thing: This is indeed a C library, written in C89 and works in C. The OOP appearance, such as SubStrings.Length(), is function pointer trickery for cleanliness' sake. SubStrings is also a non-hosted library, meaning it has no dependencies, and can thus even be used in your bootloader's source code.
String copy and concatenation, truly safe
SubStrings copy and concatenation functions have precise bounds checking, and always result in a null terminated string, and, if the size parameter is given accurately, SubStrings never has buffer overflows. Let's illustrate.
#include "substrings/substrings.h"
void MyFunc(void)
{
char Array[1024];//So, the maximum string data copied will be sizeof Array - 1, so there is room for the '\0'.
SubStrings.Copy(Array, "My string is awesome!", sizeof Array);/*SubStrings.Cat's size parameter needs to be the *maximum capacity* of the destination. You don't need to subtract from
the max size each iteration of a loop. SubStrings knows how to do it.*/
SubStrings.Cat(Array, " And it merges really nice too!", sizeof Array);
}
strcpy and strcat() are in general, unsafe, and strncat and strncpy have differing and confusing behavior. SubStrings eliminates these problems with one consistent approach for concatenation and copy operations.
All the necessities provided
SubStrings also provides all the other functions you might want for basic string operations, including Length(), Compare(), NCompare() [analogous to strncmp()], Find(), and CFind() [Find a single character], and FindAnyOf() [Analogous to strchr()].
Find() and CFind() accept another, new argument, allowing you to directly request the N-th occurrence of the matching string/characters. There is also IsLowerS, IsLowerC, IsUpperC, and conversion functions like LowerS, LowerC, UpperS, and UpperC.
In general, functions ending in S deal with strings, and functions ending in C deal with single characters.
High level stuff is here too
Some of the cooler stuff is stuff you might expect to find in Python.
Stuff like StartsWith, EndsWith, Replace, Strip, Reverse, and StripLeadingChars and StripTrailingChars.
And some original ideas too
There are other functions, like Extract(), which pulls the string content that's in between two sequences.
Let's have an example.
#include "substrings/substrings.h"
int main(int argc, char **argv)
{
int Inc = 1;
char Buf[256];for (; Inc < argc; ++Inc)
{
if (SubStrings.StartsWith("--config=", argv[Inc]))
{
SubStrings.Extract(Buf, sizeof Buf, "=", NULL, argv[Inc]);
DoSomething(Buf);
}
}
}
What's happening here is that Extract is pulling the data that starts after the =, and since the next parameter is NULL, it reads on until the end of the string. This makes handling command line arguments marginally simpler.
There's other goodies, like SubStrings.CopyUntil(). Let's take a look.
#include "substrings/substrings.h"
void MyFunc(void)
{
const char *const String = "Wibble[END]Nurble[END]Aburble[END]Farts";
const char *Iter = String;
char Buf[256];
while (SubStrings.CopyUntil(Buf, sizeof Buf, &Iter, "[END]", true))
{
puts(Buf);
}
}
This produces the output:
Wibble
Nurble
Aburble
Farts
I actually find myself using CopyUntil and its sister function CopyUntilC quite often. Then there's SubStrings.Line.GetLine(), which is a specialized CopyUntil that helps with processing multi-line C strings.
Lastly, there's another useful function, Split().
#include "substrings/substrings.h"
void MyFunc(void)
{
const char *String = "Gerbil|Wibble";
char One[256], Two[256];SubStrings.Split(One, Two, "|", String, SPLIT_HALFONE);
}
Now, One[] contains "Gerbil|" and Two[] contains "Wibble". You can specify to discard the split tokens, or put them in half one or two. The options are SPLIT_NOKEEP, SPLIT_HALFONE, and SPLIT_HALFTWO. Because Split() doesn't ask for buffer sizes for the sake of convenience, the way to do it safely is to make sure that both One and Two will be able to hold the entire length of String, if needed.
This library is getting more touchups. You can find the github here, and the SubStrings homepage here.
Thoughts, ideas or suggestions? Let me know.
Over at the other place, when I get a link to a comment, the comment provides no context about the story other than the headline. Here, a link to a comment shows the story as well. Kudos to the guys who made all this work, from little things like that to big things like Unicode support. (And I'm sure bigger stuff than that, but I'm not really educated about it.)
If you haven't shown SN a little love financially maybe think about some of those little details that have been so nicely handled. They took time and work on somebody's part. The least the rest of us can do is help keep the lights on around here.
Well, it finally happened. Last night, I had sex with someone new for the first time in 13 years.
I've been seeing this girl for about a month now, and it's been going surprisingly well. I've been seeing her once a week and she has been coming over to my place and I make her dinner and then we cuddle on the couch and talk. Last night we moved things to the bedroom.
It wasn't as awkward as I though it was going to be. After having sex with the same person for 13 years, I was really nervous to experience someone new. Anyways, I guess sex is sex. It all felt familiar, so it didn't feel as weird as I thought.
I haven't used a condom in a LONG time, and I remember how much condoms suck. I needed to pick up some non-latex ones, so that might be a factor. I also think that the condom was maybe too small, and also I think that it might be better if I add some lube to the inside. If anyone has a suggestion of a good non-latex condom, please recommend below.
We cuddled in bed after and talked, which was really nice.
After she left, I changed she sheets on the bed, and waited for my wife to come home. When she did some home, it wasn't really a big deal at all. I told her how things went (basically what I said above) and that I had already changed the sheets. We hugged, and then it was back to the routine. It really was like nothing had changed. (Thank god!)
We have been talking about this for so long, that it was nice to actually have something happen and see that everything is still okay. I showered before bed, and then cuddled with my wife as we fell asleep -- just like normal.
So far, so good.
Most people don't celebrate St. Martin's Day, but we make it a family holiday. Martin of Tours is one of our family heroes, even though we come from a church that typically doesn't honor saints of ages past. We honor Martin for his pacifism, and we also honor Martin for his belief that secular authorities should not enforce religious laws, certainly not with execution! You can read more about Martin here if you are interested.
It's nice that Martin's day occurs on Armistice Day, which used to be a day for celebrating the one thing about war worth celebrating: when it ends.
Expounding on what I feel ban-worthy as linked in a recent story I subbed...
These are my opinions not site policy. That said, they are also my minimum requirements to remain on staff.
Over-the-top spam: This means dozens of spam comments to a single story, automated or not. Anything less can and should be dealt with by simply modding the comments as Spam.
Gross/repeated illegal activity: Linking to copyrighted works and other illegality of the minor variety that we notice should be resolved by editing the comment in the database and letting the user know why their comment was edited in a reply. Bans should be reserved for things such as illegal and credible threats or multiple instances of minor illegal activity that was not ceased when notified that it should be.
Opinions, truly held or of a trollish nature: Absolutely never should this be criteria for a ban.
The meaning of "site bans": Banning an account or IP address from posting to the website. I don't have as much issue with IRC bans, it's a secondary means of communication for us not our primary one.
Any further clarification or other questions, feel free to ask.
I plan to buy a house next spring, so I'll almost certainly need a new refrigerator. There's a problem: they don't make the fridge I want, and never have. I can't figure out why.
Refrigerators today are quite different than antique ones, using a different coolent because of the ozone layer, better insulation, the use of rare earth magnets in the motors, and other improvements.
But they're still incredibly wasteful.
The fridge I want has two vents outside, much like dryer vents but insulated. There is an electronic outside thermometer, one in the refrigerator, and one inside the freezer.
When the temperature outside is above seventy fareignheight, the heat taken from the fridge is vented outside, so the air conditioner doesn't have to work harder to cool the hot air refrigerators let out inside the house.
Under seventy the air is vented into, rather than outside, the house. If the heat is on, it doesn't work as hard.
But most of all is winter. It's ludicrous that we pump the heat from our freezers with a lot of energy expenditure, while freezing air is right outside that could come in the intake hose and freeze and cool your food. At freezing, this fridge doesn't need the compressor at all and compressors take a lot of energy to operate.
I don't know why nobody is selling those things.
So, our annual family Remember the Fifth of November fireworks have been delayed two nights in a row due to rain. So what do we wind up doing? The kids come over while I'm washing dishes, look at my laptop on the counter, and see a tab opened to keep track of Bitcoin's current price. They start watching it change and asking questions and before I know it I've explained how a commodities exchange works, along with a lot about supply and demand.
Boy we are a bunch of geeks! (And I love it.)
[A]narchy is in fact the only political position that is actually possible. I believe that all other political states are in fact variations or outgrowths of a basic state of anarchy; after all, when you mention the idea of anarchy to most people they will tell you what a bad idea it is because the biggest gang would just take over. Which is pretty much how I see contemporary society. We live in a badly developed anarchist situation in which the biggest gang has taken over and have declared that it is not an anarchist situation—that it is a capitalist or a communist situation. But I tend to think that anarchy is the most natural form of politics for a human being to actually practice. All it means, the word, is no leaders. An-archon. No leaders.
-- Alan Moore
Thanks, Alan.