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-170.mta0.migadu.com (out-170.mta0.migadu.com [91.218.175.170]) by mail-a.sr.ht (Postfix) with ESMTPS id 839782008A for <~johnnyrichard/olang-devel@lists.sr.ht>; Mon, 19 Feb 2024 00:20:09 +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=1708302008; 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=AX4EjMVDIfBKUSoIaugbb0ZU8UJzstyyovNXNM+VB5kqIRcIZmkKXc2zSrwZNVV7Li1NNu 4IqmWDQwTazSX6EgkmIdlsp0frLMeztjcM6tHvf3wYJWAHKTS8dC5nYvFApeF76tnLDIQu C6NFpDXO65jRPo1j3X+vOcCTNxTQqjYmhy2JzfLut0B+HQwj8D6yUxoXNhRvBvNjEaKgnB xTeFSX3jREtKwwB43Rv44gPprhfGaUJxGsXS3Dw14T5nXPKCGtLQWHALLv5CYZG2VnKxe5 QjUib8X8i6pSug0n+5rbD0JS7kiht+rcRpP19Te13OUUb8ScEVcFE2o1oETUFw== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang 1/2] utils: create string_view data structure Date: Mon, 19 Feb 2024 02:15:38 +0100 Message-ID: <20240219011835.14769-2-johnny@johnnyrichard.com> In-Reply-To: <20240219011835.14769-1-johnny@johnnyrichard.com> References: <20240219011835.14769-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-TUID: Q24Gt2lUHeth 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