⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string_misc.pm

📁 对文件或目录的操作函数模版.根据对目录的分析结果对目录进行深度或广度遍历从而操作文件(或目录)
💻 PM
📖 第 1 页 / 共 2 页
字号:
        $pos += $temp;
        return $pos;
    }

    while ($foreSubStr =~ m:\':g) {
        $openQuoted = !$openQuoted;
    }

    if ($openQuoted) {
        $rearSubStr = substr($line, $pos);
        $temp = 0;
        $temp = pos($rearSubStr) if ($rearSubStr =~ m:\':g);
        $pos += $temp;
        return $pos;
    }

    return 0;
}

# this is for C language: the delimiter are classified into 2 categoris:
#   one is "//" the other is "/* */". This subroutine judge whether a
# delimiter is enclosed with one kind of the delimiters.
sub isCommentDelimiter()
{
    my ($line, $delimiter, $innerDelimiter, $ret) = @_;

    #if no $line or no $delimiter return 0.
    return 0 if (!$line || !$delimiter);

    $innerDelimiter = '\/\/' if ($delimiter eq '/*');
    $innerDelimiter = '\/\*' if ($delimiter eq '//');

    return pos($line) if ($line =~ m/$innerDelimiter/g);

    #this extra branch is for string like this [a = 1; //str..//nest comment]
    if ($delimiter eq '//') {
        return pos($line) if ($line =~ m:\/\/:g);
    }

    return 0;
}

# calculate the nest bracket-"{" count.
# param0
#   -[in]: a perl language line without comment.
# param1
#   -[in|out]: when as a [in] param it is the nest bracket level before the
#         line while as a [out] param it is the level after this line is parsed.
# return
#    the last bracket position in this line.
sub calBracket()
{
    my ($line, $refCount) = @_;
    my ($openBracketPos, $closureBracketPos, $temp) = (0, 0);

    return 0 if (!$line or !$refCount);

    return 0 if (ref($refCount) ne "SCALAR");

    $temp = $line;
    $openBracketPos = pos($temp) if ($temp =~ m\{\g);
    $temp = $line;
    $closureBracketPos = pos($temp) if ($temp =~ m\}\g);

    return 0 if (0 == $openBracketPos && 0 == $closureBracketPos);

    if (0 == $openBracketPos) {
        --$$refCount;
        $line = substr($line, $closureBracketPos);
        while ($line =~ m/}/g) {
            $temp = pos($line);
            $closureBracketPos += $temp;
            --$$refCount;
            $line = substr($line, $temp);
        }

        return $closureBracketPos;
    }
    elsif (0 == $closureBracketPos){
        ++$$refCount;
        $line = substr($line, $openBracketPos);
        while ($line =~ m/{/g) {
            $temp = pos($line);
            $openBracketPos += $temp;
            ++$$refCount;
            $line = substr($line, $temp);
        }

        return $openBracketPos;
    }

    if ($openBracketPos < $closureBracketPos) {
        #the first bracket is open bracket.
        $temp = substr($line, $openBracketPos);
        ++$$refCount;
        return  $openBracketPos + &calBracket($temp, \$$refCount);
    }
    else {
        #the first bracket is closure bracket.
        $temp = substr($line, $closureBracketPos);
        --$$refCount;
        return $closureBracketPos + &calBracket($temp, \$$refCount);
    }
}

# estimate whethere the C file comment delimiter "//", "/*" or "*/" is quoted.
#param
#   -[in]: the whole line string where comment delimiter lies in.
#   -[in]: the delimiter: "//", "/*" or "*/".
#return value
#   - 0: the delimiter is not quoted.
#   - else return the position of the second quotation mark in the whole line.
sub isQuotedDelimiter()
{
    my ($line, $delimiter) = @_;
    my ($foreSubStr, $rearSubStr, $singleQuote, $temp, $ret);

    #if no $line or no $delimiter return 0.
    return 0 if (!$line || !$delimiter);

    $delimiter = '\/\*' if ($delimiter eq '/*');
    $delimiter = '\/\/' if ($delimiter eq '//');

    #return 0 if $line contains no $delimiter.
    if ($line =~ /$delimiter/g) {
        #why use //g midifier? 'Cause to loacte the delimiter with pos().
        $ret = pos($line);
        $foreSubStr = substr($line, 0, $ret);
    }
    return 0 if (!$foreSubStr);

    $singleQuote = 0;
    while ($foreSubStr =~ m:\":g) {
        $singleQuote = !$singleQuote;
    }

    if ($singleQuote) {
        $rearSubStr = substr($line, $ret);
        $temp = 0;
        while ($rearSubStr =~ m:\":g) {
            $temp = pos($rearSubStr);
        }
        $ret += $temp;
        return $ret;
    }

    while ($foreSubStr =~ m:\':g) {
        $singleQuote = !$singleQuote;
    }

    if ($singleQuote) {
        $rearSubStr = substr($line, $ret);
        $temp = 0;
        while ($rearSubStr =~ m:\':g) {
            $temp = pos($rearSubStr);
        }
        $ret += $temp;
        return $ret;
    }

    return 0;
}

# C language commnet: "//" --> "/* any comment */". So comply with ansi.
#
# param0
#   - [in]: a open file handle.
# param1
#   - [in]: a referenced string.
# param2
#   - [in]: whether the line is being with comment which succeesive to last line.
#
# return value:
#   - bCommentFromBegin
sub convertslash2asterisk()
{
    my ($fileHandle, $line, $bCommentFromBegin, $temp) = @_;

    if ($bCommentFromBegin) {
        # comment_from_last_line */int a;
        if ($line =~ m:^(.*)\*\/(.*):g) {
            $temp = $1 . "\*\/";
            print $fileHandle $temp;

            $line = $2 . "\n";
            $bCommentFromBegin = 0;
            goto section if ($line);
            return $bCommentFromBegin;
        }
        else {
            print $fileHandle $line;
            return 1;
        }
    }
    else {
        # "    "//this is comment
        if ($line =~ m/^(\s*)\/\/(.*)/) {
            print $fileHandle $1 if $1;
            print $fileHandle '/*';
            if ($2) {
                $temp = $2;
                if ($temp =~ m/\*\/$/) {
                    print $fileHandle "$temp\n";
                }
                else {
                    print $fileHandle "$temp\*\/\n";
                }
            }
            else {
                print $fileHandle "\*\/\n";
            }

            return 0;
        }

        # "http//:www.google.com" or "every string // .. other". In brief,
        # a "//" in the ["  "] or in [' '].
        $temp = &isQuotedDelimiter($line, '//');
        if ($temp) {
            print $fileHandle substr($line, 0, $temp);
            $line = substr($line, $temp);
            goto section;
        }

        # printf("/* this is a comment example"). In brief,
        # a "/*" in the ["  "] or in [' '].
        $temp = &isQuotedDelimiter($line, '/*');
        if ($temp) {
            print $fileHandle substr($line, 0, $temp);
            $line = substr($line, $temp);
            goto section;
        }

        # int a; ... //
        if ($line =~ m:^(.*)\/\/(.*):g) {
            if ($1) {
                $temp = &isCommentDelimiter($1, '//');
                if ($temp) {
                    if ($1 =~ m/^\/\//) {
                        # "//abc" --> "/*abc"
                        substr($line, 1, 1) = '*';
                    }
                    else {
                        # "abc; //123 ..//any .."  -> "abc; /*123 ..//any .."
                        substr($line, $temp -1, 1) = '*';
                    }

                    if (not $line =~ m/\*\/\n$/) {
                        substr($line, length($line) - 1, 1) = "\*\/\n";
                    }
                    print $fileHandle $line;
                    return 0;
                }
                else {
                    print $fileHandle "$1";
                }
            }

            if ($2) {
                $temp = "\/\*" . $2;
                if ($2 =~ m/\*\/$/) {
                    $temp .= "\n";
                }
                else {
                    $temp .= "\*\/\n";
                }
                print $fileHandle $temp;
            }
            else {
                print $fileHandle "\n";
            }

            return 0;
        }

        # int a; ... /* some comment. */
        if ($line =~ m:^(.*)\/\*(.*):g) {
            print $fileHandle $1 if ($1);

            print $fileHandle "\/\*";

            $bCommentFromBegin = 1;
            if ($2) {
                $line = $2 . "\n";
                goto section;
            }
            else {
                print $fileHandle "\n";
                return $bCommentFromBegin;
            }
        }

        print $fileHandle "$line";
        return 0;
    }

section:
    &convertslash2asterisk($fileHandle, $line, $bCommentFromBegin);
}

return 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -