将string.endswith方法转换为C [英] Conversion of string.endswith method into C

查看:53
本文介绍了将string.endswith方法转换为C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始一个个人项目,将用python编写的解释器转换为C.这纯粹是出于学习目的.

I am beginning a personal project of converting an interpreter written in python into C. It is purely for learning purposes.

我遇到的第一件事是尝试转换以下内容:

The first thing I have come across is trying to convert the following:

if __name__ == "__main__":
    if not argv[-1].endswith('.py'):
        ...

到目前为止,我已经为 endswith 方法

And I have done the following conversion thus far for the endswith method

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool endswith(char* str, char* substr)
{
    // case1: one of the strings is empty
    if (!str || !substr) return false;

    char* start_of_substring = strstr(str, substr);

    // case2: not in substring
    if (!start_of_substring) return false;

    size_t length_of_string    = strlen(str);
    size_t length_of_substring = strlen(substr);
    size_t index_of_match      = start_of_substring - str;

    // case2: check if at end
    return (length_of_string == length_of_substring + index_of_match);

}

int main(int argc, char* argv[])
{
    char *last_arg = argv[argc-1];
    if (endswith(last_arg, ".py")) {
        // ...
    } 

}

这看起来是否涵盖了 endswith 中的所有案例,还是我错过了一些边缘案例?如果是这样,该如何改善?最后,这不是批评,而是编写C应用程序时的真正问题:编写C比在python中做相同的事情是否需要5至10倍的代码是常见的?而且不知道该怎么做?)

Does this look like it's covering all the cases in an endswith, or am I missing some edge cases? If so, how can this be improved and such? Finally, this isn't a criticism but more a genuine question in writing a C application: is it common that writing C will require 5-10x more code than doing the same thing in python (or is that more because I'm a beginner and don't know how to do things properly?)

及其相关: https://codereview.stackexchange.com/questions/54722/determine-if-one-string-occurs-at-the-end-of-another/54724

推荐答案

这看起来像是涵盖了所有情况吗?还是我错过了一些边缘情况?

Does this look like it's covering all the cases in an endswith, or am I missing some edge cases?

您至少缺少子字符串出现两次或两次以上(末尾出现之一)的情况.

You are missing at least the case where the substring appears twice or more, one of the appearances at the end.

我不会为此使用 strstr().相反,我将根据两个字符串的相对长度来确定要在主字符串中查找的位置,然后使用 strcmp().示例:

I wouldn't use strstr() for this. Instead, I would determine from the relative lengths of the two strings where in the main string to look, and then use strcmp(). Example:

bool endswith(char* str, char* substr) {
    if (!str || !substr) return false;

    size_t length_of_string    = strlen(str);
    size_t length_of_substring = strlen(substr);

    if (length_of_substring > length_of_string) return false;

    return (strcmp(str + length_of_string - length_of_substring, substr) == 0);
}

关于该 return 语句: str + length_of_string-length_of_substring 等效于& str [length_of_string-length_of_substring] -是,指向尾随子字符串的第一个字符的指针,其长度与 substr 相同. strcmp 函数比较两个C字符串,根据第一个参数在字典上小于,等于或大于第二个参数,返回小于,等于或大于零的整数.特别是, strcmp()的参数相等时将返回0,并且此函数将返回完全相同的测试结果.

With regard to that return statement: str + length_of_string - length_of_substring is equivalent to &str[length_of_string - length_of_substring] -- that is, a pointer to the first character of the trailing substring the same length the same length as substr. The strcmp function compares two C strings, returning an integer less than, equal to, or greater than zero depending on whether the first argument is lexicographically less than, equal to, or greater than the second. In particular, strcmp() returns 0 when its argument are equal, and this function returns the result of exactly such a test.

与在python中做相同的事情相比,编写C所需的代码多出5至10倍是常见的情况

is it common that writing C will require 5-10x more code than doing the same thing in python

Python是比C更高级的语言,因此对于同一任务,C语言的任务通常比Python代码更长.而且,C块被明确地定界,这使得C代码比Python代码更长.不过,我不确定5-10倍是一个不错的估计,我认为在这种情况下,您是将苹果与桔子进行比较.类似于Python代码的代码就是

Python is a higher-level language than C, so it is common for C code for a task to be lengthier than Python code for the same task. Also, that C blocks are explcitly delimited makes C code a little longer than Python code. I'm not sure that 5-10x is a good estimate, though, and I think that in this case you're comparing apples to oranges. The code analogous to your Python code is simply

int main(int argc, char* argv[]) {
    if (endswith(argv[argc-1], ".py")) {
        // ...
    } 
}

C没有内置的 endswith()函数是另一回事.

That C has no built-in endswith() function is a separate matter.

这篇关于将string.endswith方法转换为C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆