From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mail-a.sr.ht; dkim=pass header.d=johnnyrichard.com header.i=@johnnyrichard.com Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [95.215.58.177]) by mail-a.sr.ht (Postfix) with ESMTPS id 90525200D9 for <~johnnyrichard/olang-devel@lists.sr.ht>; Mon, 19 Feb 2024 20:07:03 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1708373223; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Szi8Cr7Ld5aJuv/S98UyQCfXiVB1v5o2ns3zREiHYbc=; b=lu3txpIfzqtuM6CiUEF82JnsfohorZQLzt+VIWXESYOHpteBwFldJ3KXk2+CnHnBpKW5vo HR0Wezo76QXR1umzDjrsfIP+jZT6VPL5IhOlskmp5+9/DbB2C4k0egj/ueIiW+1X924N2N GyD2+zjqQtKWReA0oxKva9EfjSCJC8fFjSU9N/w9DeweyipOHLHZyZw02/A9cD54crVadI yLCSlpLasdAjOcY89APm1RuUPalbl6BB3C2U7Q/VT+TflBhQsoEWePxyKv4wMGiABPM9ph vHBSlwuZ40Pyq000Pe+Vdz6IcX/LgNoHFf4pC/sh7+MjbfXJ67BJ7CeIKKVbqA== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v4 1/4] utils: create string_view data structure Date: Mon, 19 Feb 2024 22:04:08 +0100 Message-ID: <20240219210541.25624-2-johnny@johnnyrichard.com> In-Reply-To: <20240219210541.25624-1-johnny@johnnyrichard.com> References: <20240219210541.25624-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-TUID: EQDIDMXyW8Bv Signed-off-by: Johnny Richard --- src/string_view.c | 35 +++++++++++++++++++++++++++++++++++ src/string_view.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/string_view.c create mode 100644 src/string_view.h diff --git a/src/string_view.c b/src/string_view.c new file mode 100644 index 0000000..122eaa2 --- /dev/null +++ b/src/string_view.c @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2024 olang maintainers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "string_view.h" + +#include +#include + +bool +string_view_eq_to_cstr(string_view_t str, char *cstr) +{ + size_t cstr_len = strlen(cstr); + if (str.size != cstr_len) { + return false; + } + + size_t i = 0; + while (i < cstr_len && str.chars[i] == cstr[i]) { + i++; + } + return i == cstr_len; +} diff --git a/src/string_view.h b/src/string_view.h new file mode 100644 index 0000000..367ef6b --- /dev/null +++ b/src/string_view.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2024 olang maintainers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef STRING_VIEW_T +#define STRING_VIEW_T + +#include +#include + +typedef struct string_view +{ + char *chars; + size_t size; + +} string_view_t; + +// TODO: missing unit test +bool +string_view_eq_to_cstr(string_view_t str, char *cstr); + +#endif /* STRING_VIEW_T */ -- 2.43.2