Search Results

Search found 3 results on 1 pages for 'meeselet'.

Page 1/1 | 1 

  • Why no switch on pointers?

    - by meeselet
    For instance: #include <stdio.h> void why_cant_we_switch_him(void *ptr) { switch (ptr) { case NULL: printf("NULL!\n"); break; default: printf("%p!\n", ptr); break; } } int main(void) { void *foo = "toast"; why_cant_we_switch_him(foo); return 0; } gcc test.c -o test test.c: In function 'why_cant_we_switch_him': test.c:5: error: switch quantity not an integer test.c:6: error: pointers are not permitted as case values Just curious. Is this a technical limitation? EDIT People seem to think there is only one constant pointer expression. Is that is really true, though? For instance, here is a common paradigm in Objective-C (it is really only C aside from NSString, id and nil, which are merely a pointers, so it is still relevant — I just wanted to point out that there is, in fact, a common use for it, despite this being only a technical question): #include <stdio.h> #include <Foundation/Foundation.h> static NSString * const kMyConstantObject = @"Foo"; void why_cant_we_switch_him(id ptr) { switch (ptr) { case kMyConstantObject: // (Note that we are comparing pointers, not string values.) printf("We found him!\n"); break; case nil: printf("He appears to be nil (or NULL, whichever you prefer).\n"); break; default: printf("%p!\n", ptr); break; } } int main(void) { NSString *foo = @"toast"; why_cant_we_switch_him(foo); foo = kMyConstantObject; why_cant_we_switch_him(foo); return 0; } gcc test.c -o test -framework Foundation test.c: In function 'why_cant_we_switch_him': test.c:5: error: switch quantity not an integer test.c:6: error: pointers are not permitted as case values It appears that the reason is that switch only allows integral values (as the compiler warning said). So I suppose a better question would be to ask why this is the case? (though it is probably too late now.)

    Read the article

  • Is str.replace(..).replace(..) ad nauseam a standard idiom in Python?

    - by meeselet
    For instance, say I wanted a function to escape a string for use in HTML (as in Django's escape filter): def escape(string): """ Returns the given string with ampersands, quotes and angle brackets encoded. """ return string.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace("'", '&#39;').replace('"', '&quot;') This works, but it gets ugly quickly and appears to have poor algorithmic performance (in this example, the string is repeatedly traversed 5 times). What would be better is something like this: def escape(string): """ Returns the given string with ampersands, quotes and angle brackets encoded. """ # Note that ampersands must be escaped first; the rest can be escaped in # any order. return replace_multi(string.replace('&', '&amp;'), {'<': '&lt;', '>': '&gt;', "'": '&#39;', '"': '&quot;'}) Does such a function exist, or is the standard Python idiom to use what I wrote before?

    Read the article

  • How to remove the file extension in a zsh completion?

    - by meeselet
    I want to adjust zsh so that I can tab complete: myprog <tab> using all *.foo files in ~/somedir, but have it so that it displays them without the .foo extension. Is there any way to do this? This is what I have so far: #compdef myprog typeset -A opt_args local context state line local -a mydirs mydirs="(. ~/somedir)" _arguments -s -S \ "*:name:->foos" \ && return 0 case $state in (foos) _files -W ${mydirs} -g '*.foo(:r)' && return 0 ;; esac return 1 However, this displays double the output for every file (that is, each .foo file is listed with and without its extension). Is there any way around this?

    Read the article

1