From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id 8480D6DE1202 for ; Mon, 21 Aug 2017 08:45:01 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: -0.311 X-Spam-Level: X-Spam-Status: No, score=-0.311 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.211, SPF_PASS=-0.001] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HYk6PrV1uOUJ for ; Mon, 21 Aug 2017 08:45:00 -0700 (PDT) Received: from mail-wr0-f196.google.com (mail-wr0-f196.google.com [209.85.128.196]) by arlo.cworth.org (Postfix) with ESMTPS id D392A6DE1152 for ; Mon, 21 Aug 2017 08:44:59 -0700 (PDT) Received: by mail-wr0-f196.google.com with SMTP id f8so13177969wrf.3 for ; Mon, 21 Aug 2017 08:44:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=ThlEbwu0eTMGCuXTGYogfHO8cttpXk33Oh7W4LrbXpE=; b=TNOugp1fWWh/72Zz0/0mX9wfxDatVc/IVamC5TINMS8qjoWe+viOWTooBhuVARe9IW dhYPz/nmkG2VkKp1IJrXxp3cTdH26zzVzHuwrR/KV0g/JrCxL9R5GJdWlZdy0Ayao5ml +32u3Tge5dvg2DuevYGI7po+6ylNdG8jjJfzgkGY9jjZ+/fQyUsRuHzZdfPCvtttCtaX cBF+VPQNnieyFJM41kPKqcdmk1fIzANRqVoVBDggynJLLF9QIE78oZeqcYCXpSevjNMF pHgCYraaKZmtbDQ8jf8g1ARY1A+JgdNsmkMvdvNWlv/tUXYiPbXQqqMNyRCYL9xWFUbF I6Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=ThlEbwu0eTMGCuXTGYogfHO8cttpXk33Oh7W4LrbXpE=; b=iDzprepUAs0Xf2ZoENKHSteuRHBSYsK918nPf9wWxO7yojqACmbmYJtwMebgFaOoZm 6C+h57ov/MtNsfi5FaeQO6PZT2hDEKBC064flci9oLP4XdoZtqYqwkbmQuJaVQvDPdFi SKDCweH244ix3xlrbwCnyZ4hRMgznKYUEgaxCPiI0NBhnQfCIa/53cGbmDRlDDvBudkR yGaL7qmpiRupCIWIDBf28onaDsCd0EzIL3SrTZEcsIrJl7MnvEUJMz9GnzYsJ4myD2gg iIGyFXRBvtaC4UejhLBLzfzDUE7Scpu7LcTpoZeLWsXYKhyxrX6W3FySWVnqjMVfE/ZH sPMg== X-Gm-Message-State: AHYfb5hY4f5sgH1i63II0oBCPp6aO1pVtqsoQEJUXBNK33Plg0a75Jzv ssn11NY6GD3qhdxe4Uo= X-Received: by 10.28.68.214 with SMTP id r205mr4871678wma.125.1503330297210; Mon, 21 Aug 2017 08:44:57 -0700 (PDT) Received: from localhost (mito.neclab.eu. [195.37.70.39]) by smtp.gmail.com with ESMTPSA id l5sm6513406wrg.93.2017.08.21.08.44.56 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Aug 2017 08:44:56 -0700 (PDT) From: Yuri Volchkov To: notmuch@notmuchmail.org Subject: [PATCH v2 1/4] database: move striping of trailing '/' into helper function Date: Mon, 21 Aug 2017 17:44:45 +0200 Message-Id: <1503330288-9375-2-git-send-email-yuri.volchkov@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1503330288-9375-1-git-send-email-yuri.volchkov@gmail.com> References: <1502556453-11609-1-git-send-email-yuri.volchkov@gmail.com> <1503330288-9375-1-git-send-email-yuri.volchkov@gmail.com> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Aug 2017 15:45:01 -0000 Stripping trailing character is not that uncommon operation. Particularly, the next patch has to perform it as well. Lets move it to the separate function to avoid code duplication. Also the new function has a little improvement: if the character to strip is repeated several times in the end of a string, function strips them all. Signed-off-by: Yuri Volchkov --- lib/database.cc | 3 +-- util/string-util.c | 13 +++++++++++++ util/string-util.h | 2 ++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/database.cc b/lib/database.cc index 8f0e22a..79eb3d6 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -858,8 +858,7 @@ notmuch_database_open_verbose (const char *path, notmuch->status_string = NULL; notmuch->path = talloc_strdup (notmuch, path); - if (notmuch->path[strlen (notmuch->path) - 1] == '/') - notmuch->path[strlen (notmuch->path) - 1] = '\0'; + strip_trailing(notmuch->path, '/'); notmuch->mode = mode; notmuch->atomic_nesting = 0; diff --git a/util/string-util.c b/util/string-util.c index 1812530..b010881 100644 --- a/util/string-util.c +++ b/util/string-util.c @@ -255,3 +255,16 @@ strcase_hash (const void *ptr) return hash; } + +void +strip_trailing (char *str, char ch) +{ + int i; + + for (i = strlen (str) - 1; i >= 0; i--) { + if (str[i] == ch) + str[i] = '\0'; + else + break; + } +} diff --git a/util/string-util.h b/util/string-util.h index 87917b8..9777061 100644 --- a/util/string-util.h +++ b/util/string-util.h @@ -75,6 +75,8 @@ int strcase_equal (const void *a, const void *b); /* GLib GHashFunc compatible case insensitive hash function */ unsigned int strcase_hash (const void *ptr); +void strip_trailing (char *str, char ch); + #ifdef __cplusplus } #endif -- 2.7.4