簡體   English   中英

在SystemVerilog中搜索和替換字符串

[英]String search and replace in SystemVerilog

在SystemVerilog中進行字符串搜索和替換的最簡單方法是什么?

例如,我有:

string hdl_path = "DUT.my_red39";

如何創建一個新string ,將紅色替換為藍色

EDA游樂場鏈接

您可以使用最近創建的SystemVerilog實用程序庫之一。

ClueLib

svlib

或者,您可以通過比較各個字符來使用普通的SystemVerilog來完成此操作:

  function automatic string search_replace(string original, string old, string replacement);
    // First find the index of the old string
    int start_index = 0;
    int original_index = 0;
    int replace_index = 0;
    bit found = 0;

    while(1) begin
      if (original[original_index] == old[replace_index]) begin
        if (replace_index == 0) begin
          start_index = original_index;
        end
        replace_index++;
        original_index++;
        if (replace_index == old.len()) begin
          found = 1;
          break;
        end
      end else if (replace_index != 0) begin
        replace_index = 0;
        original_index = start_index + 1;
      end else begin
        original_index++;
      end
      if (original_index == original.len()) begin
        // Not found
        break;
      end
    end

    if (!found) return original;

    return {
      original.substr(0, start_index-1),
      replacement,
      original.substr(start_index+old.len(), original.len()-1)
    };

  endfunction

EDA Playground上的SystemVerilog替換示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM