2016-08-29 89 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() 
相關問題