From patchwork Wed Apr 4 13:57:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 895009 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-91418-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="j6NG8JLb"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40GSFq6wnKz9s0b for ; Wed, 4 Apr 2018 23:57:35 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:to:subject:mime-version:content-type :content-transfer-encoding:message-id:from; q=dns; s=default; b= w2xng7SMkRlbOnpjHWytM0QHRjlje0EwpU1922Gu4XRz4vrmqjV44DqWs0wwSzI7 QFj7q8T2fghpUUddw3yg3uZNvH/cOlanbAFDxETzYmucWj+cF1BSe5nNwjvbVBI6 h05xRyr4jHqzMlg8PlUjTZyHrAzSyd+FYIoUxQL8Ouo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:to:subject:mime-version:content-type :content-transfer-encoding:message-id:from; s=default; bh=Dz4V4X XbwtlIGKpaZwUBmoegbcQ=; b=j6NG8JLbF1co/Si05dPsDoquK7s0BVhlNQo+MN WVxgQ5I3DU4aTTE4kDyzBKYIHo/o/RgG12jswy9wEQoibQ79zATKtM5T8iKBXFUs J9heiUhJ8W2O5nQXBoZ3zxSPcnYoEJrR//LTMyu8vpiLx9h+J2NS6VRPc6m/92eH M5TV0= Received: (qmail 60167 invoked by alias); 4 Apr 2018 13:57:29 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 60154 invoked by uid 89); 4 Apr 2018 13:57:29 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Date: Wed, 04 Apr 2018 15:57:24 +0200 To: libc-alpha@sourceware.org Subject: [PATCH] manual: Move mbstouwcs to an example C file User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Message-Id: <20180404135724.B1B56406F5A23@oldenburg.str.redhat.com> From: fweimer@redhat.com (Florian Weimer) 2018-04-04 Florian Weimer * manual/examples/mbstouwcs.c: New file. * manual/charset.texi (Converting a Character): Include it. diff --git a/manual/charset.texi b/manual/charset.texi index 1867ace485..6831ebec27 100644 --- a/manual/charset.texi +++ b/manual/charset.texi @@ -686,28 +686,7 @@ converting all lowercase characters into uppercase could look like this checking, and sometimes leaks memory): @smallexample -wchar_t * -mbstouwcs (const char *s) -@{ - size_t len = strlen (s); - wchar_t *result = malloc ((len + 1) * sizeof (wchar_t)); - wchar_t *wcp = result; - wchar_t tmp[1]; - mbstate_t state; - size_t nbytes; - - memset (&state, '\0', sizeof (state)); - while ((nbytes = mbrtowc (tmp, s, len, &state)) > 0) - @{ - if (nbytes >= (size_t) -2) - /* Invalid input string. */ - return NULL; - *wcp++ = towupper (tmp[0]); - len -= nbytes; - s += nbytes; - @} - return result; -@} +@include mbstouwcs.c.texi @end smallexample The use of @code{mbrtowc} should be clear. A single wide character is diff --git a/manual/examples/mbstouwcs.c b/manual/examples/mbstouwcs.c new file mode 100644 index 0000000000..5d223da2ae --- /dev/null +++ b/manual/examples/mbstouwcs.c @@ -0,0 +1,28 @@ +#include +#include +#include + +/* Do not include the above headers in the example. +*/ +wchar_t * +mbstouwcs (const char *s) +{ + size_t len = strlen (s); + wchar_t *result = malloc ((len + 1) * sizeof (wchar_t)); + wchar_t *wcp = result; + wchar_t tmp[1]; + mbstate_t state; + size_t nbytes; + + memset (&state, '\0', sizeof (state)); + while ((nbytes = mbrtowc (tmp, s, len, &state)) > 0) + { + if (nbytes >= (size_t) -2) + /* Invalid input string. */ + return NULL; + *wcp++ = towupper (tmp[0]); + len -= nbytes; + s += nbytes; + } + return result; +}