Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp1235833rdb; Mon, 4 Sep 2023 07:13:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHLnllkbjbWCgY/v9+R4gJxLAK0nDBnTHAecvt8JweZxZvMYRTQ0uYulv84yIJJm7stjU1R X-Received: by 2002:a05:6e02:2195:b0:34b:aa34:a5c4 with SMTP id j21-20020a056e02219500b0034baa34a5c4mr12555853ila.30.1693836818411; Mon, 04 Sep 2023 07:13:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693836818; cv=none; d=google.com; s=arc-20160816; b=UA90b74rSFVUCBwZdKPRJEo7Ko79dQhmV1zfmjh69aS6TXsaIIs/M9C9V3Qo76O3ql RwzKen4QFVehCfimc/4bPMVsFsfZlfHv6XMJybO6gDaa3eIiuSlM4vSIQCwI0ic5m8fR K27Vzfe/U8/39yNHEQReiAEzdaRysQtTNgNyekwIBUZ8fFg/L9ZT4/mVY5fMAJZeHN92 RBHBxWveqOwDl4oVF+J3J4d08X7pr5WlAYLws861vf4CNXHhG6Imyc2WTqiddAz5AD6j uoPstKqwYISWO+ZNoXcJLLcyf10FBNnDqTw3WRX1j5LSyc0RINBYZiVf2F6J7sYcqQ49 g5CA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=psIx59g2lba6Bj41pmw9vzztX0lse/mgh428SeWXOeo=; fh=v2hp+ajopg+aEBvDbqX80Ysctn2jAXWeaxJHXHVR728=; b=nEX8kY4hgYkafmHJYb13DiLOLmxglaPftFgof+NMoMURnEkxqR7Kuf1s3+n0wyrJco lInauWletB8wM19bHag0VZvRghkzENan9WVUy64yFJjW95J2sBprT88bfxHUmmQGksLs ul/Tkz6EGRj7xWc1e6/jLbbwpk3JoQXyj1WPOXox3IFewRKc85Wt17VJ/VHs6yzYj1Sd 2eogAydaacnIfyHm7CrSWWDaw5TW0pzmo1us8aQd934v1uiM237gVMC6+C5hC8EsoPJu 4WlJdIQA67nUnmxe7izw2/BIOwvYdIuux0tpAunPhfxnHi7zZ12Q6n8Rc/w2TCJeMDcN E8cw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w17-20020a639351000000b0056fa94ba999si7762870pgm.197.2023.09.04.07.13.09; Mon, 04 Sep 2023 07:13:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352703AbjIDJw7 (ORCPT + 99 others); Mon, 4 Sep 2023 05:52:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238277AbjIDJw4 (ORCPT ); Mon, 4 Sep 2023 05:52:56 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7CAED10F5; Mon, 4 Sep 2023 02:52:47 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3DDBC1042; Mon, 4 Sep 2023 02:53:25 -0700 (PDT) Received: from localhost.localdomain (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id EB2EC3F793; Mon, 4 Sep 2023 02:52:43 -0700 (PDT) From: James Clark To: linux-perf-users@vger.kernel.org, irogers@google.com Cc: James Clark , John Garry , Will Deacon , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Adrian Hunter , Kan Liang , Haixin Yu , Jing Zhang , Kajol Jain , Madhavan Srinivasan , Ravi Bangoria , Yang Jihong , Eduard Zingerman , Chen Zhongjin , Liam Howlett , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v2 3/7] perf util: Add a function for replacing characters in a string Date: Mon, 4 Sep 2023 10:50:45 +0100 Message-Id: <20230904095104.1162928-4-james.clark@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230904095104.1162928-1-james.clark@arm.com> References: <20230904095104.1162928-1-james.clark@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It finds all occurrences of a single character and replaces them with a multi character string. This will be used in a test in a following commit. Reviewed-by: Ian Rogers Signed-off-by: James Clark --- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/tests.h | 1 + tools/perf/tests/util.c | 31 +++++++++++++++++++++ tools/perf/util/string.c | 48 +++++++++++++++++++++++++++++++++ tools/perf/util/string2.h | 1 + 6 files changed, 83 insertions(+) create mode 100644 tools/perf/tests/util.c diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index 63d5e6d5f165..2b45ffa462a6 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build @@ -66,6 +66,7 @@ perf-y += dlfilter-test.o perf-y += sigtrap.o perf-y += event_groups.o perf-y += symbols.o +perf-y += util.o ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 arm arm64 powerpc)) perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index 0ad18cf6dd22..cb6f1dd00dc4 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -123,6 +123,7 @@ static struct test_suite *generic_tests[] = { &suite__sigtrap, &suite__event_groups, &suite__symbols, + &suite__util, NULL, }; diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index f33cfc3c19a4..b394f3ac2d66 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -145,6 +145,7 @@ DECLARE_SUITE(dlfilter); DECLARE_SUITE(sigtrap); DECLARE_SUITE(event_groups); DECLARE_SUITE(symbols); +DECLARE_SUITE(util); /* * PowerPC and S390 do not support creation of instruction breakpoints using the diff --git a/tools/perf/tests/util.c b/tools/perf/tests/util.c new file mode 100644 index 000000000000..6366db5cbf8c --- /dev/null +++ b/tools/perf/tests/util.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "tests.h" +#include "util/debug.h" + +#include +#include +#include + +static int test_strreplace(char needle, const char *haystack, + const char *replace, const char *expected) +{ + char *new = strreplace_chars(needle, haystack, replace); + int ret = strcmp(new, expected); + + free(new); + return ret == 0; +} + +static int test__util(struct test_suite *t __maybe_unused, int subtest __maybe_unused) +{ + TEST_ASSERT_VAL("empty string", test_strreplace(' ', "", "123", "")); + TEST_ASSERT_VAL("no match", test_strreplace('5', "123", "4", "123")); + TEST_ASSERT_VAL("replace 1", test_strreplace('3', "123", "4", "124")); + TEST_ASSERT_VAL("replace 2", test_strreplace('a', "abcabc", "ef", "efbcefbc")); + TEST_ASSERT_VAL("replace long", test_strreplace('a', "abcabc", "longlong", + "longlongbclonglongbc")); + + return 0; +} + +DEFINE_SUITE("util", util); diff --git a/tools/perf/util/string.c b/tools/perf/util/string.c index cf05b0b56c57..116a642ad99d 100644 --- a/tools/perf/util/string.c +++ b/tools/perf/util/string.c @@ -301,3 +301,51 @@ unsigned int hex(char c) return c - 'a' + 10; return c - 'A' + 10; } + +/* + * Replace all occurrences of character 'needle' in string 'haystack' with + * string 'replace' + * + * The new string could be longer so a new string is returned which must be + * freed. + */ +char *strreplace_chars(char needle, const char *haystack, const char *replace) +{ + int replace_len = strlen(replace); + char *new_s, *to; + const char *loc = strchr(haystack, needle); + const char *from = haystack; + int num = 0; + + /* Count occurrences */ + while (loc) { + loc = strchr(loc + 1, needle); + num++; + } + + /* Allocate enough space for replacements and reset first location */ + new_s = malloc(strlen(haystack) + (num * (replace_len - 1) + 1)); + if (!new_s) + return NULL; + loc = strchr(haystack, needle); + to = new_s; + + while (loc) { + /* Copy original string up to found char and update positions */ + memcpy(to, from, 1 + loc - from); + to += loc - from; + from = loc + 1; + + /* Copy replacement string and update positions */ + memcpy(to, replace, replace_len); + to += replace_len; + + /* needle next occurrence or end of string */ + loc = strchr(from, needle); + } + + /* Copy any remaining chars + null */ + strcpy(to, from); + + return new_s; +} diff --git a/tools/perf/util/string2.h b/tools/perf/util/string2.h index 56c30fef9682..52cb8ba057c7 100644 --- a/tools/perf/util/string2.h +++ b/tools/perf/util/string2.h @@ -39,5 +39,6 @@ char *strpbrk_esc(char *str, const char *stopset); char *strdup_esc(const char *str); unsigned int hex(char c); +char *strreplace_chars(char needle, const char *haystack, const char *replace); #endif /* PERF_STRING_H */ -- 2.34.1