On Tue, Mar 20, 2001 at 06:25:30PM -0700, Warner Losh wrote:
> In message <[EMAIL PROTECTED]> "Michael C . Wu" writes:
> : Thinker thinks that memset() is too costly to use here
> : to modify one or two bytes. I agreed with him in that
> : filenames can't be that long to justify the memset()
> : overhead. However, with today's CPU power, I think
> : memset()'s overhead will only be noticeable with a
> : large directory filled with data. Please tell Thinker
> : what you think.
>
> It is mostly a conceptual thing. You are setting memory or copying
> memory. You should use the API for that rather than roll your own.
I don't know what does it make different. I think it likes adding
a comment at right of variable 'len' to tell people it is meaning 'length'.
Should we crazy functionize every thing? Every one have a choice.
No matter what, the code should be accepted by most people here. I
provide another version as another choice.
---------- begin ---------------
--- util.c.orig Sun Mar 18 16:35:12 2001
+++ util.c Wed Mar 21 07:29:51 2001
@@ -60,15 +60,41 @@
prn_printable(s)
const char *s;
{
- unsigned char c;
- int n;
+ const char *p; /* String walker. */
+ char *r, *ri; /* Ptr for result string & walker of it. */
+ int len;
+ int dc; /* Count down of length after 'p' . */
+ size_t sz; /* Number of bytes been processed. */
+ wchar_t c;
- for (n = 0; (c = *s) != '\0'; ++s, ++n)
- if (isprint(c))
- putchar(c);
- else
- putchar('?');
- return n;
+ if (s == NULL)
+ return (0);
+ p = s;
+ dc = len = strlen(s);
+ ri = r = (char *)malloc(len + 1);
+ if (r == NULL)
+ return (0);
+
+ while (dc > 0) {
+ sz = mbtowc(&c, p, dc);
+ if (sz < 0) { /* Not be recognized. */
+ p++;
+ dc--;
+ *ri++ = '?';
+ } else {
+ if (isprint(c))
+ memcpy(ri, p, sz);
+ else
+ memset(ri, (int)'?', sz);
+ ri += sz;
+ p += sz;
+ dc -= sz;
+ }
+ }
+ *ri = '\0';
+ printf("%s", r);
+ free(r);
+ return (len);
}
/*
---------- end -----------------
--
[EMAIL PROTECTED] Branda Open Site (BOS)
[EMAIL PROTECTED] http://www.branda.to/
To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-current" in the body of the message