GNU make manual 翻译( 一百八十六)

继续翻译

8.2 Functions for String Substitution and Analysis
==================================================

Here are some functions that operate on strings:

`$(subst FROM,TO,TEXT)'
     Performs a textual replacement on the text TEXT: each occurrence
     of FROM is replaced by TO.  The result is substituted for the
     function call.  For example,

          $(subst ee,EE,feet on the street)

     substitutes the string `fEEt on the strEEt'.

`$(patsubst PATTERN,REPLACEMENT,TEXT)'
     Finds whitespace-separated words in TEXT that match PATTERN and
     replaces them with REPLACEMENT.  Here PATTERN may contain a `%'
     which acts as a wildcard, matching any number of any characters
     within a word.  If REPLACEMENT also contains a `%', the `%' is
     replaced by the text that matched the `%' in PATTERN.  Only the
     first `%' in the PATTERN and REPLACEMENT is treated this way; any
     subsequent `%' is unchanged.

     `%' characters in `patsubst' function invocations can be quoted
     with preceding backslashes (`\').  Backslashes that would
     otherwise quote `%' characters can be quoted with more backslashes.
     Backslashes that quote `%' characters or other backslashes are
     removed from the pattern before it is compared file names or has a
     stem substituted into it.  Backslashes that are not in danger of
     quoting `%' characters go unmolested.  For example, the pattern
     `the\%weird\\%pattern\\' has `the%weird\' preceding the operative
     `%' character, and `pattern\\' following it.  The final two
     backslashes are left alone because they cannot affect any `%'
     character.

     Whitespace between words is folded into single space characters;
     leading and trailing whitespace is discarded.

     For example,

          $(patsubst %.c,%.o,x.c.c bar.c)

     produces the value `x.c.o bar.o'.

     Substitution references (*note Substitution References:
     Substitution Refs.) are a simpler way to get the effect of the
     `patsubst' function:

          $(VAR:PATTERN=REPLACEMENT)

     is equivalent to

          $(patsubst PATTERN,REPLACEMENT,$(VAR))

     The second shorthand simplifies one of the most common uses of
     `patsubst': replacing the suffix at the end of file names.

          $(VAR:SUFFIX=REPLACEMENT)

     is equivalent to

          $(patsubst %SUFFIX,%REPLACEMENT,$(VAR))

8.2 字符串替换和分析函数
==================================================

这里有一些操作字符串的函数:

`$(subst FROM,TO,TEXT)' 在文本TEXT 上完成文本替换:每一次发现FROM,就替换为TO。

在函数调用的时候,结果替换完毕。例如,

$(subst ee,EE,feet on the street)

替换结果为: `fEEt on the strEEt'。

找到在TEXT中被空格分隔的,匹配PATTERN的单词,用REPLACEMENT 来替换它们。这里 PATTERN 可以包含一个%,%是一个通配符浩,匹配在一个单词中的任意多个的单词。如果REPLACEMENT 中也包含%,那么%被匹配PATTERN的文本替换。只有PATTERN和 REPLACEMENT 中的第一个% 才会被如此处理;任何后续的% 都不会被修改。

在patsubst 函数运行中,%符号可以被用前导的反斜线来引用。反斜线也可以被反斜线引用。此反斜线在被用来参与比较运算或者枝干替换之前就会被模式去除。

不影响到%字符的反斜线不会导致麻烦。例如 模式 'the\%weird\\%pattern\\' 在 操作性的%符号之前,有 'the%weird\', 后面跟着  'pattern\\'。最后的两个反斜线被单独扔下,因为它们不会影响到任何%符号。

在单词中的空格会被转换为单个空格,而前面的后面的空格会被除掉。

例如,$(patsubst %.c,%.o,x.c.c bar.c) 产生的结果是:`x.c.o bar.o'.

替换参照是一个 (*note Substitution References:
Substitution Refs.) 获得和 patsubst 函数一样效果的方式:
`patsubst' function:

$(VAR:PATTERN=REPLACEMENT)

等价于

$(patsubst PATTERN,REPLACEMENT,$(VAR))

第二个快捷方式简化了最普通的对pstsubst的用法,在文件名的结尾替换了前缀。

$(VAR:SUFFIX=REPLACEMENT)

等价于

$(patsubst %SUFFIX,%REPLACEMENT,$(VAR))

后文待续

posted @ 2012-10-09 14:56  健哥的数据花园  阅读(348)  评论(0编辑  收藏  举报