[hackers] [quark] Support encoding UTF-8 characters || FRIGN
commit 29d53f65b70dd8f4ed2ae54ac17877dce29c8cf9 Author: FRIGN AuthorDate: Mon Sep 5 20:10:16 2016 +0200 Commit: FRIGN CommitDate: Mon Sep 5 20:10:16 2016 +0200 Support encoding UTF-8 characters There's no loss doing it so, as FF = 255 and the upper half of the unsigned char range is exactly where we feel at home with UTF-8. diff --git a/quark.c b/quark.c index 2c03271..1faad5d 100644 --- a/quark.c +++ b/quark.c @@ -172,7 +172,7 @@ decode(char src[PATH_MAX], char dest[PATH_MAX]) if (*s == '+') { dest[i] = ' '; } else if (*s == '%' && (sscanf(s + 1, "%2hhx", &n) == 1)) { - dest[i] = (char)(n & 255); + dest[i] = n; s += 2; } else { dest[i] = *s; @@ -191,7 +191,7 @@ encode(char src[PATH_MAX], char dest[PATH_MAX]) for (s = src, i = 0; *s; s++) { if (isalnum(*s) || *s == '~' || *s == '-' || *s == '.' || - *s == '_') { + *s == '_' || *s > 127) { i += snprintf(dest + i, PATH_MAX - i, "%%%02X", *s); } else { dest[i] = *s;
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
sizeof var (note the lack of parens) is a feature in c. It lets the programmer put whatever type var is in one place and one place only. The thing that got me away from putting parentheses to sizeof was actually the fact that only in the absolutely rarest of cases (read: never) you take the size of a thing that is not already in a scope variable you're working on. If you get a type passed somewhere else, have the size passed as a size_t argument and stick with that. I started with C writing return() and sizeof() to have it all look the same, but that has been brought to my attention to be ignorant practise. Those are not in that sense functions, as one is an operator and the other a control statement. cheers! mar77i
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On Mon, 5 Sep 2016 10:05:16 +0200 FRIGN wrote: > [...], but I respect the per-process style. Of course I meant "per-project style". -- FRIGN
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On Mon, 5 Sep 2016 09:53:57 +0200 Anselm R Garbe wrote: Hey Anselm, > That's wrong. There are good reasons like forward declarations / > opaque type definitions that incorporate typedef. how are you using the term forward declaration in this context? Are you referring to function prototypes with "simpler" arguments? As to opaque type definitions: There are very few cases where this actually makes sense, especially in a language like C. Opaque types often "present" themselves when developing toolboxes for numerical issues, but often the incentives are not too high. In dwm/dmenu the convention is to have the first type-letter uppercase, like "Display". This is imho a good naming convention in itself and does not require a new "format" for sizeof without parentheses if you are so inclined to use typedefs for your structs. I don't see a good reason to use typedefs in dwm/dmenu in the first place, but I respect the per-process style. Cheers FRIGN -- FRIGN
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On Mon, 5 Sep 2016 09:55:49 +0200 Anselm R Garbe wrote: Hey Anselm, > Beware! There have been zealots arguing that using goto is a bad > practice for similar reasons. > > typedef's have to be considered more carefully. zealots argue against goto because it's a low-level feature and they have not understood that loops are basically using goto under the hood. So we can identify the notion against goto as the fear of low-level stuff and a fear of low-abstraction. With typedefs, we are going another direction, because typedefs are a "high-abstraction" just like using if () { } else if () { if () { } else { } } trees just not to have gotos in the code. Cheers FRIGN -- FRIGN
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On 5 September 2016 at 09:52, FRIGN wrote: > On Mon, 5 Sep 2016 09:48:34 +0200 > Anselm R Garbe wrote: > Using typedefs destroys this beautiful hierarchy. Beware! There have been zealots arguing that using goto is a bad practice for similar reasons. typedef's have to be considered more carefully. -Anselm
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On 5 September 2016 at 09:50, FRIGN wrote: > On Mon, 5 Sep 2016 09:42:47 +0200 > Anselm R Garbe wrote: >> Why is typedef'ing structs bad practice? > > because there's no reason for it other than syntax candy. It also hides That's wrong. There are good reasons like forward declarations / opaque type definitions that incorporate typedef. > from the user what he is dealing with and when I want to lookup a > struct definition I often have to jungle-jump across multiple typedef > layers to finally reach the definition. Of course typedef's can over-complicate code, but they can also simplify code. I'm referring to the latter. One can find good and bad examples, imho dwm/dmenu are good examples of typedef usage. I'm just referring to those. Granted, Xlib is more of the sort bad example. Cheers, Anselm
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On Mon, 5 Sep 2016 09:48:34 +0200 Anselm R Garbe wrote: Hey Anselm, > Haha, you now open up another bad thing imho. I tried to avoid > _t-suffix crap in all my code so far. you don't have to and I also don't like the _t suffix, but it's now part of the standard. My point here is that if a "new" type is introduced to the C system, it usually will get a _t suffix by the comittee. As for non-standard types introduced by the programmer, my point is not to use typedefs at all and use the structure identifiers, including struct, enum, union and so on. Using typedefs destroys this beautiful hierarchy. Cheers FRIGN -- FRIGN
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On Mon, 5 Sep 2016 09:42:47 +0200 Anselm R Garbe wrote: Hey Anselm, > Why is typedef'ing structs bad practice? because there's no reason for it other than syntax candy. It also hides from the user what he is dealing with and when I want to lookup a struct definition I often have to jungle-jump across multiple typedef layers to finally reach the definition. If I have a program for handling bank accounts and define an account-struct like struct account { char *firstname; char *lastname; int balance; }; I think it's more descriptive in the code to declare a local variable like struct account fisher; rather than having a typdef #typedef struct account Account which yields Account fisher; It may look "nicer", "cleaner", "shorter", but it just hides information from the user. Look at the Posix socket interfaces; there's a reason why all the sock-address structs were left as-is and why a messy project like X11 typedefs the shit out of its structs. The ultimate reason is because X11 has become so complex that its authors tried to at least make it visually half-appealing by "shortening" their code with struct typedefs. If your code has become so complex and overwhelming that plain "struct structname" constructs can't be fitted in there any more you should fix it. That's my opinion. Cheers FRIGN -- FRIGN
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On 5 September 2016 at 09:44, FRIGN wrote: > On Mon, 5 Sep 2016 09:39:24 +0200 > Anselm R Garbe wrote: >> I'm referring to NOT using parentheses to make it explicitely clear, >> that the expression is not referring to a type. See sec. 6.5.3 in the >> C lang spec for further details. > > why would there ever be a doubt that "unsigned char" is a type? Even if > you use the stdint.h types, e.g. uint8_t, Posix has noticed and taken > measures to make it clear with the _t suffix that uint8_t is a type. Haha, you now open up another bad thing imho. I tried to avoid _t-suffix crap in all my code so far. Cheers, Anselm
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On Mon, 5 Sep 2016 09:39:24 +0200 Anselm R Garbe wrote: Hey Anselm, > Why should I try. This is blatantly obvious wrong code. > > I'm referring to NOT using parentheses to make it explicitely clear, > that the expression is not referring to a type. See sec. 6.5.3 in the > C lang spec for further details. why would there ever be a doubt that "unsigned char" is a type? Even if you use the stdint.h types, e.g. uint8_t, Posix has noticed and taken measures to make it clear with the _t suffix that uint8_t is a type. As I mentioned earlier, if you cannot discern a type name from a variable name it's your problem and should not be "fixed" by having a strange use of sizeof which is error-prone, as Ali has well shown. This obviously goes the other way around too. One should not use variable names that can be misinterpreted as a typename. Cheers FRIGN -- FRIGN
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On 5 September 2016 at 09:41, FRIGN wrote: > On Mon, 5 Sep 2016 09:37:17 +0200 > Anselm R Garbe wrote: >> I disagree. Not using parentheses as sizeof arguments makes it pretty >> clear, that the expression is *not* about the size of a type, but >> rather needs to be evaluated. In dwm/dmenu code this has been >> respected until now for a very long time. I would keep this principle >> in place for other suckless tools as well. > > but this only happens if you typedef your structs, which I think is bad > practice. Why is typedef'ing structs bad practice? -Anselm
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On 2016-09-05 10:39, Anselm R Garbe wrote: Why should I try. This is blatantly obvious wrong code. and that's my point.
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On Mon, 5 Sep 2016 09:37:17 +0200 Anselm R Garbe wrote: Hey Anselm, > I disagree. Not using parentheses as sizeof arguments makes it pretty > clear, that the expression is *not* about the size of a type, but > rather needs to be evaluated. In dwm/dmenu code this has been > respected until now for a very long time. I would keep this principle > in place for other suckless tools as well. but this only happens if you typedef your structs, which I think is bad practice. If you do a #typedef struct hw homework this is your own fault. Using sizeof(struct hw) shows clearly it's a type, whereas sizeof(homework) is not clear. But this is more a criticism of extreme typedeffing. Stop using typedefs for structs and you won't have this problem any more. If you can't discern type names from variable names, this is your problem. Cheers FRIGN -- FRIGN
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On 5 September 2016 at 09:34, Ali H. Fardan wrote: > I got an example: > try compiling this code: > > #include > > int > main(void) > { > printf("%d", sizeof unsigned char); > return (0); > } Why should I try. This is blatantly obvious wrong code. I'm referring to NOT using parentheses to make it explicitely clear, that the expression is not referring to a type. See sec. 6.5.3 in the C lang spec for further details. Cheers, Anselm
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On 5 September 2016 at 09:25, FRIGN wrote: > On Mon, 5 Sep 2016 07:42:36 +0200 > Anselm R Garbe wrote: >> Quick note: your syntax usage of sizeof is not 100% accurate. >> >> Use 'sizeof(type)' with brackets but 'sizeof var' without. > > I use sizeof always function-like and see no reason why I shouldn't use > "sizeof(var)" and instead use "sizeof var". It doesn't alter the code > behaviour and ultimately it's all about readability. > If you eyes have become accustomed to always use function-like syntax > for such operators the best bet is not to break this style because it's > not "necessary" to have parentheses for variable sizeof's. I disagree. Not using parentheses as sizeof arguments makes it pretty clear, that the expression is *not* about the size of a type, but rather needs to be evaluated. In dwm/dmenu code this has been respected until now for a very long time. I would keep this principle in place for other suckless tools as well. Cheers, Anselm
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
I got an example: try compiling this code: #include int main(void) { printf("%d", sizeof unsigned char); return (0); } you will probably get a compiler error like this: sizeof.c: In function ‘main’: sizeof.c:6:22: error: expected expression before ‘unsigned’ printf("%d", sizeof unsigned char); ^~~~ However, with parenthesies: #include int main(void) { printf("%d\n", sizeof(unsigned char)); return (0); } It will compile successfully. Hope you got the idea Raiz On 2016-09-05 10:25, FRIGN wrote: On Mon, 5 Sep 2016 07:42:36 +0200 Anselm R Garbe wrote: Hey Anselm, Quick note: your syntax usage of sizeof is not 100% accurate. Use 'sizeof(type)' with brackets but 'sizeof var' without. I use sizeof always function-like and see no reason why I shouldn't use "sizeof(var)" and instead use "sizeof var". It doesn't alter the code behaviour and ultimately it's all about readability. If you eyes have become accustomed to always use function-like syntax for such operators the best bet is not to break this style because it's not "necessary" to have parentheses for variable sizeof's. Cheers FRIGN
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On Mon, 5 Sep 2016 07:42:36 +0200 Anselm R Garbe wrote: Hey Anselm, > Quick note: your syntax usage of sizeof is not 100% accurate. > > Use 'sizeof(type)' with brackets but 'sizeof var' without. I use sizeof always function-like and see no reason why I shouldn't use "sizeof(var)" and instead use "sizeof var". It doesn't alter the code behaviour and ultimately it's all about readability. If you eyes have become accustomed to always use function-like syntax for such operators the best bet is not to break this style because it's not "necessary" to have parentheses for variable sizeof's. Cheers FRIGN -- FRIGN
Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN
On 2016-09-05 08:53, Markus Teich wrote: can you elaborate on the reasoning behind this? The styleguide says "Always use () with sizeof". from my experience, using sizeof without parenthesies could cause errors. Raiz