2016-08-29 43 views

回答

0

找到最大的通用前缀通常是在两个字符串开始的O(n)线性搜索中完成的。 CMake允许使用if command和LESS/GREATER/..比较数字和字符串。这允许实现寻找最大通用前缀的标准方式:

function(largest_common_prefix a b prefix) 

# minimum of lengths of both strings 
string(LENGTH ${a} len_a) 
string(LENGTH ${a} len_b) 

if(${len_a} LESS ${len_b}) 
    set(len ${len_a}) 
else() 
    set(len ${len_b}) 
endif() 

# iterate over the length 
foreach(end RANGE 1 ${len}) 
    # get substrings 
    string(SUBSTRING ${a} 0 ${end} sub_a) 
    string(SUBSTRING ${b} 0 ${end} sub_b) 

    # if equal store, otherwise break 
    if (${sub_a} STREQUAL ${sub_b}) 
     set(${prefix} ${sub_a} PARENT_SCOPE) 
    else() 
     break() 
    endif() 
endforeach() 

endfunction() 
相关问题