String.st
changeset 8415 7e0f1105bc9f
parent 8383 dea5311899c5
child 8438 317f70e84d61
equal deleted inserted replaced
8414:dca52c2725ef 8415:7e0f1105bc9f
  2802 
  2802 
  2803 printfPrintString:formatString
  2803 printfPrintString:formatString
  2804     "non-standard but sometimes useful.
  2804     "non-standard but sometimes useful.
  2805      Return a printed representation of the receiver as specified by formatString, 
  2805      Return a printed representation of the receiver as specified by formatString, 
  2806      which is defined by printf.
  2806      which is defined by printf.
  2807      No checking on buffer overrun is done; the resulting string may not
       
  2808      be larger than 799 characters.
       
  2809      This method is NONSTANDARD and may be removed without notice."
  2807      This method is NONSTANDARD and may be removed without notice."
  2810 
  2808 
  2811 %{  /* STACK: 1000 */
  2809 %{  /* STACK: 1000 */
  2812 
  2810 
  2813     char buffer[800];
  2811     char buffer[800];
       
  2812     char *buf = buffer;
       
  2813     int bufsize = sizeof(buffer);
       
  2814     char *mallocbuf = NULL;
  2814     char *cp;
  2815     char *cp;
       
  2816     int len;
  2815     OBJ s;
  2817     OBJ s;
  2816 
  2818 
  2817     if (__isString(formatString)) {
  2819     if (__isString(formatString)) {
       
  2820         cp = (char *)__stringVal(self);
       
  2821         if (__qClass(self) != String)
       
  2822             cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars));
       
  2823 
       
  2824 again:
  2818         /*
  2825         /*
  2819          * actually only needed on sparc: since thisContext is
  2826          * actually only needed on sparc: since thisContext is
  2820          * in a global register, which gets destroyed by printf,
  2827          * in a global register, which gets destroyed by printf,
  2821          * manually save it here - very stupid ...
  2828          * manually save it here - very stupid ...
  2822          */
  2829          */
  2823 
       
  2824         cp = (char *)__stringVal(self);
       
  2825         if (__qClass(self) != String)
       
  2826             cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars));
       
  2827 
       
  2828         __BEGIN_PROTECT_REGISTERS__
  2830         __BEGIN_PROTECT_REGISTERS__
  2829 
  2831 
  2830         sprintf(buffer, (char *)__stringVal(formatString), cp);
  2832         len = snprintf(buf, bufsize, (char *)__stringVal(formatString), cp);
  2831 
  2833 
  2832         __END_PROTECT_REGISTERS__
  2834         __END_PROTECT_REGISTERS__
  2833 
  2835 
  2834         s = __MKSTRING(buffer COMMA_SND);
  2836 
       
  2837         if (len > bufsize) {
       
  2838             bufsize = len + 1;
       
  2839             if (mallocbuf)
       
  2840                 free(mallocbuf);
       
  2841             buf = mallocbuf = malloc(bufsize);
       
  2842             if (buf == NULL)
       
  2843                 goto fail;
       
  2844             goto again;
       
  2845         } else if (len < 0) {
       
  2846             bufsize *= 2;
       
  2847             if (mallocbuf)
       
  2848                 free(mallocbuf);
       
  2849             buf = mallocbuf = malloc(bufsize);
       
  2850             if (buf == NULL)
       
  2851                 goto fail;
       
  2852             goto again;
       
  2853         }
       
  2854 
       
  2855         s = __MKSTRING_L(buf, len);
       
  2856 
       
  2857         if (mallocbuf)
       
  2858             free(mallocbuf);
       
  2859 
  2835         if (s != nil) {
  2860         if (s != nil) {
  2836             RETURN (s);
  2861             RETURN (s);
  2837         }
  2862         }
  2838     }
  2863     }
       
  2864 fail:;
  2839 %}.
  2865 %}.
  2840     self primitiveFailed
  2866     self primitiveFailed
  2841 
  2867 
  2842     "
  2868     "
  2843      'hello' printfPrintString:'%%s -> %s'     
  2869      'hello' printfPrintString:'%%s -> %s'     
       
  2870      (String new:900) printfPrintString:'%%s -> %s'     
  2844      'hello' printfPrintString:'%%10s -> %10s'  
  2871      'hello' printfPrintString:'%%10s -> %10s'  
  2845      'hello' printfPrintString:'%%-10s -> %-10s' 
  2872      'hello' printfPrintString:'%%-10s -> %-10s' 
       
  2873      'hello' printfPrintString:'%%900s -> %900s' 
       
  2874      'hello' printfPrintString:'%%-900s -> %-900s' 
  2846     "
  2875     "
  2847 !
  2876 !
  2848 
  2877 
  2849 storeOn:aStream
  2878 storeOn:aStream
  2850     "put the storeString of myself on aStream"
  2879     "put the storeString of myself on aStream"
  3301     ^ aRequestor traceString:self level:level from:referrer
  3330     ^ aRequestor traceString:self level:level from:referrer
  3302 
  3331 
  3303 
  3332 
  3304 ! !
  3333 ! !
  3305 
  3334 
       
  3335 
  3306 !String class methodsFor:'documentation'!
  3336 !String class methodsFor:'documentation'!
  3307 
  3337 
  3308 version
  3338 version
  3309     ^ '$Header: /cvs/stx/stx/libbasic/String.st,v 1.206 2004-06-08 10:29:55 cg Exp $'
  3339     ^ '$Header: /cvs/stx/stx/libbasic/String.st,v 1.207 2004-06-26 12:13:51 stefan Exp $'
  3310 ! !
  3340 ! !