{"thread":{"id":"6879","subject":"Numeric constants as strings","startedAt":"2007-02-19T09:16:34Z","lastAt":"2007-02-20T13:06:42Z","messageCount":11,"participants":["Andy Parkins","Junio C Hamano","Shawn O. Pearce","Matthieu Moy","Mark Wooding","Jakub Narebski"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"34987","messageId":"200702190916.35813.andyparkins@gmail.com","threadId":"6879","inReplyTo":null,"subject":"Numeric constants as strings","fromName":"Andy Parkins","fromEmail":"andyparkins@gmail.com","sentAt":"2007-02-19T09:16:34Z","receivedAt":"2007-02-19T09:16:34Z","isPatch":false,"sender":{"key":"andyparkins@gmail.com","avatar":null},"body":"Hello,\n\nI'm working on bringing my hash width literals patch up to date now that 1.5.0 \nhas passed.  It's all been trivial apart from one line:\n\n#define HASH_WIDTH_ASCII 40\n-               printf(\"%-40s %s%s (%d subtrees)\\n\",\n+               printf(\"%-\" HASH_WIDTH_ASCII \"s %s%s (%d subtrees)\\n\",\n\nThis compiles, but I suspect that it's not going to do what I want it to do.  \nIt's something I've never been able to do in C - how does one get quotes \naround a defined value?  I don't really want to resort to\n\n+               printf(\"%-*s %s%s (%d subtrees)\\n\", HASH_WIDTH_ASCII,\n\nAs that's a runtime change.\n\n\n\nAndy\n-- \nDr Andy Parkins, M Eng (hons), MIEE\nandyparkins@gmail.com\n"},{"id":"34989","messageId":"7virdy8p25.fsf@assigned-by-dhcp.cox.net","threadId":"6879","inReplyTo":"200702190916.35813.andyparkins@gmail.com","subject":"Re: Numeric constants as strings","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-02-19T09:38:42Z","receivedAt":"2007-02-19T09:38:42Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Andy Parkins <andyparkins@gmail.com> writes:\n\n> I'm working on bringing my hash width literals patch up to\n> date now that 1.5.0 has passed.  It's all been trivial apart\n> from one line:\n>\n> #define HASH_WIDTH_ASCII 40\n> -               printf(\"%-40s %s%s (%d subtrees)\\n\",\n> +               printf(\"%-\" HASH_WIDTH_ASCII \"s %s%s (%d subtrees)\\n\",\n>\n> This compiles, but I suspect that it's not going to do what I\n> want it to do.\n\nDoesn't writing \"foo\" \"bar\" (two string literals next to each other)\ntell the compiler to concatenate them?\n"},{"id":"34991","messageId":"7vbqjq8ojr.fsf@assigned-by-dhcp.cox.net","threadId":"6879","inReplyTo":"200702190916.35813.andyparkins@gmail.com","subject":"Re: Numeric constants as strings","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-02-19T09:49:44Z","receivedAt":"2007-02-19T09:49:44Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Andy Parkins <andyparkins@gmail.com> writes:\n\n> I'm working on bringing my hash width literals patch up to\n> date now that 1.5.0 has passed.\n\nI do not want to risk discouraging public discussion on this\ntopic, but I am not sure if this is really worth it.\n\nIt is not like if/when we find SHA-1 is inadequate we would just\nswitch to SHA-256 and redefine HASH_BYTES from 20 to 32 and be\ndone with it.  With the need for backward compatibility, we\nwould probably end up changing \"unsigned char sha1[20]\" to\nsomething that allows us to tell which hash function's result we\nare talking about, like:\n\n\tstruct {\n\t       \tenum { SHA_1, SHA_256 } type;\n               \tunion {\n                        unsigned char sha1[20];\n                        unsigned char sha256[32];\n\t\t} u;\n\t};\n"},{"id":"34992","messageId":"20070219094955.GE30030@spearce.org","threadId":"6879","inReplyTo":"7virdy8p25.fsf@assigned-by-dhcp.cox.net","subject":"Re: Numeric constants as strings","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-02-19T09:49:55Z","receivedAt":"2007-02-19T09:49:55Z","isPatch":false,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Junio C Hamano <junkio@cox.net> wrote:\n> Andy Parkins <andyparkins@gmail.com> writes:\n> \n> > I'm working on bringing my hash width literals patch up to\n> > date now that 1.5.0 has passed.  It's all been trivial apart\n> > from one line:\n> >\n> > #define HASH_WIDTH_ASCII 40\n> > -               printf(\"%-40s %s%s (%d subtrees)\\n\",\n> > +               printf(\"%-\" HASH_WIDTH_ASCII \"s %s%s (%d subtrees)\\n\",\n> >\n> > This compiles, but I suspect that it's not going to do what I\n> > want it to do.\n> \n> Doesn't writing \"foo\" \"bar\" (two string literals next to each other)\n> tell the compiler to concatenate them?\n\nYes, but here HASH_WIDTH_ASCII is a number...  wtf is the compiler\ngenerating for the following?\n\n  printf(\"%-\" 40 \"s %s%s (%d subtrees)\\n\",\n\nI did not even realize that was legal C...  Now if the 40 was in\nquotes (e.g. \"40\") then the concatenate rule would apply and we\nwould get a nice argument to printf.\n\n-- \nShawn.\n"},{"id":"34993","messageId":"vpq1wkm31q4.fsf@olympe.imag.fr","threadId":"6879","inReplyTo":"20070219094955.GE30030@spearce.org","subject":"Re: Numeric constants as strings","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@imag.fr","sentAt":"2007-02-19T10:01:39Z","receivedAt":"2007-02-19T10:01:39Z","isPatch":false,"sender":{"key":"git@matthieu-moy.fr","avatar":"https://avatars.githubusercontent.com/u/14709?v=4"},"body":"\"Shawn O. Pearce\" <spearce@spearce.org> writes:\n\n> I did not even realize that was legal C...  Now if the 40 was in\n> quotes (e.g. \"40\") then the concatenate rule would apply and we\n> would get a nice argument to printf.\n\nI suppose the solution is to use #HASH_WIDTH_ASCII to tell the\npreprocessor to put the quotes around HASH_WIDTH_ASCII.\n\n-- \nMatthieu\n"},{"id":"34994","messageId":"slrnetitgs.sbe.mdw@metalzone.distorted.org.uk","threadId":"6879","inReplyTo":"200702190916.35813.andyparkins@gmail.com","subject":"Re: Numeric constants as strings","fromName":"Mark Wooding","fromEmail":"mdw@distorted.org.uk","sentAt":"2007-02-19T10:04:12Z","receivedAt":"2007-02-19T10:04:12Z","isPatch":false,"sender":{"key":"mdw@distorted.org.uk","avatar":null},"body":"Andy Parkins <andyparkins@gmail.com> wrote:\n\n> I'm working on bringing my hash width literals patch up to date now\n> that 1.5.0 has passed.  It's all been trivial apart from one line:\n>\n> #define HASH_WIDTH_ASCII 40\n> -               printf(\"%-40s %s%s (%d subtrees)\\n\",\n> +               printf(\"%-\" HASH_WIDTH_ASCII \"s %s%s (%d subtrees)\\n\",\n\nThe standard two-step goes\n\n#define STRINGIFY(foo) STRINGIFY_REALLY(foo)\n#define STRINGIFY_REALLY(foo) #foo\n\nThe purpose of STRINGIFY_REALLY is to macro-expand the argument foo.  If\nyou just invoked STRINGIFY_REALLY\n\n  printf(\"%-\" STRINGIFY_REALLY(HASH_WIDTH_ASCII) \"s %s%s (%d subtrees)\\n\", \n\nyou'd get\n\n  printf(\"%-HASH_WIDTH_ASCIIs %s%s (%d subtrees)\\n\", ...\n\nwhich is not what you wanted.  However,\n\n  printf(\"%-\" STRINGIFY(HASH_WIDTH_ASCII) \"s %s%s (%d subtrees)\\n\", \n\nwill do the right thing.\n\n-- [mdw]\n"},{"id":"34995","messageId":"200702191014.23055.andyparkins@gmail.com","threadId":"6879","inReplyTo":"7virdy8p25.fsf@assigned-by-dhcp.cox.net","subject":"Re: Numeric constants as strings","fromName":"Andy Parkins","fromEmail":"andyparkins@gmail.com","sentAt":"2007-02-19T10:14:21Z","receivedAt":"2007-02-19T10:14:21Z","isPatch":false,"sender":{"key":"andyparkins@gmail.com","avatar":null},"body":"On Monday 2007 February 19 09:38, Junio C Hamano wrote:\n\n> > #define HASH_WIDTH_ASCII 40\n> > -               printf(\"%-40s %s%s (%d subtrees)\\n\",\n> > +               printf(\"%-\" HASH_WIDTH_ASCII \"s %s%s (%d subtrees)\\n\",\n> >\n> > This compiles, but I suspect that it's not going to do what I\n> > want it to do.\n>\n> Doesn't writing \"foo\" \"bar\" (two string literals next to each other)\n> tell the compiler to concatenate them?\n\nIt's not a string literal though.\n\nIt's\n #define HASH_WIDTH_ASCII 40\nrather than\n #define HASH_WIDTH_ASCII \"40\"\n\nI'm wondering how to turn the first into the second using the preprocessor.\n\n\nAndy\n-- \nDr Andy Parkins, M Eng (hons), MIEE\nandyparkins@gmail.com\n"},{"id":"34997","messageId":"200702191055.00203.andyparkins@gmail.com","threadId":"6879","inReplyTo":"vpq1wkm31q4.fsf@olympe.imag.fr","subject":"Re: Numeric constants as strings","fromName":"Andy Parkins","fromEmail":"andyparkins@gmail.com","sentAt":"2007-02-19T10:54:58Z","receivedAt":"2007-02-19T10:54:58Z","isPatch":false,"sender":{"key":"andyparkins@gmail.com","avatar":null},"body":"On Monday 2007 February 19 10:01, Matthieu Moy wrote:\n> \"Shawn O. Pearce\" <spearce@spearce.org> writes:\n> > I did not even realize that was legal C...  Now if the 40 was in\n> > quotes (e.g. \"40\") then the concatenate rule would apply and we\n> > would get a nice argument to printf.\n>\n> I suppose the solution is to use #HASH_WIDTH_ASCII to tell the\n> preprocessor to put the quotes around HASH_WIDTH_ASCII.\n\nI'm afraid that only works when you're token pasting parameters in a #define \nmacro.\n\nFor example:\n\n #define macro(x) \"foo\" #x \"baz\"\n\nThen, macro(bar) expands to \"foo\" \"bar\" \"baz\".  However, the following does \nnot work:\n\n #define BAR bar\n #define macro \"foo\" #BAR \"baz\"\n \nThis is because BAR is not a macro parameter.  I've also tried it indirectly:\n\n #define BAR bar\n #define MAKESTRING(x) #x\n #define macro \"foo\" MAKESTRING(BAR) \"baz\"\n\nBut this expands to \"foo\" \"BAR\" \"baz\".  Also wrong.  Equally, using # anywhere \nbut during a #define doesn't work, so I can't simply write\n\n printf( \"%-\" #HASH_WIDTH_ASCII \"s\", string );\n\nWoe is me. :-(\n\n\nAndy\n-- \nDr Andy Parkins, M Eng (hons), MIEE\nandyparkins@gmail.com\n"},{"id":"34998","messageId":"200702191100.42953.andyparkins@gmail.com","threadId":"6879","inReplyTo":"7vbqjq8ojr.fsf@assigned-by-dhcp.cox.net","subject":"Re: Numeric constants as strings","fromName":"Andy Parkins","fromEmail":"andyparkins@gmail.com","sentAt":"2007-02-19T11:00:37Z","receivedAt":"2007-02-19T11:00:37Z","isPatch":false,"sender":{"key":"andyparkins@gmail.com","avatar":null},"body":"On Monday 2007 February 19 09:49, Junio C Hamano wrote:\n\n> I do not want to risk discouraging public discussion on this\n> topic, but I am not sure if this is really worth it.\n\nIt's not primarily for that purpose, but rather to improve readability.\n\nFor example, this little bit of patch:\n\n-       if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\\n')\n+       if (get_sha1_hex(buffer+5, tree_sha1) || buffer[HASH_WIDTH_ASCII+5] != '\\n')\n                return objerror(&commit->object, \"invalid 'tree' line format - bad sha1\");\n-       buffer += 46;\n+       buffer += HASH_WIDTH_ASCII+6;\n        while (!memcmp(buffer, \"parent \", 7)) {\n-               if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\\n')\n+               if (get_sha1_hex(buffer+7, sha1) || buffer[HASH_WIDTH_ASCII+7] != '\\n')\n                        return objerror(&commit->object, \"invalid 'parent' line format - bad sha1\");\n-               buffer += 48;\n+               buffer += HASH_WIDTH_ASCII+8;\n\nUsing HASH_WIDTH_ASCII+8 is much clearer for a reader to be able to understand\nintent than 48.  Especially when the variables in question are called \"buffer\"\nit's harder to track what is being stored in the buffer without a named constant.\n\nThere are also a few other places where 20 is used and HASH_WIDTH_ASCII is not\nintended.\n\n\n\nAndy\n\n-- \nDr Andy Parkins, M Eng (hons), MIEE\nandyparkins@gmail.com\n"},{"id":"34999","messageId":"200702191103.00431.andyparkins@gmail.com","threadId":"6879","inReplyTo":"slrnetitgs.sbe.mdw@metalzone.distorted.org.uk","subject":"Re: Numeric constants as strings","fromName":"Andy Parkins","fromEmail":"andyparkins@gmail.com","sentAt":"2007-02-19T11:02:53Z","receivedAt":"2007-02-19T11:02:53Z","isPatch":false,"sender":{"key":"andyparkins@gmail.com","avatar":null},"body":"On Monday 2007 February 19 10:04, Mark Wooding wrote:\n\n> #define STRINGIFY(foo) STRINGIFY_REALLY(foo)\n> #define STRINGIFY_REALLY(foo) #foo\n>   printf(\"%-\" STRINGIFY(HASH_WIDTH_ASCII) \"s %s%s (%d subtrees)\\n\",\n\nAh ha!  Magical.  You've taught me something there.  Thanks.\n\n\nAndy\n\n-- \nDr Andy Parkins, M Eng (hons), MIEE\nandyparkins@gmail.com\n"},{"id":"35116","messageId":"ererl9$df0$1@sea.gmane.org","threadId":"6879","inReplyTo":"200702191103.00431.andyparkins@gmail.com","subject":"Re: Numeric constants as strings","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2007-02-20T13:06:42Z","receivedAt":"2007-02-20T13:06:42Z","isPatch":false,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Andy Parkins wrote:\n\n> On Monday 2007 February 19 10:04, Mark Wooding wrote:\n> \n>> #define STRINGIFY(foo) STRINGIFY_REALLY(foo)\n>> #define STRINGIFY_REALLY(foo) #foo\n>>   printf(\"%-\" STRINGIFY(HASH_WIDTH_ASCII) \"s %s%s (%d subtrees)\\n\",\n> \n> Ah ha!  Magical.  You've taught me something there.  Thanks.\n\nThis trick is in the cpp documentation. \n(cpp.info.gz)Stringification\n\n-- \nJakub Narebski\nWarsaw, Poland\nShadeHawk on #git\n"}]}