Velocity的坑——字符串首尾的空格那些事

在使用Velocity 1.7作为模板引擎的项目中,遇到macro生成的字符串前后出现换行和空格的问题。通过研究,找到两种解决方案:1. 将macro内所有语句写成一行并在末尾加##,但牺牲了可读性;2. 首行不缩进,输出数据行末尾加##,有效去除首尾空格。该文分享了解决此问题的经验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近的项目采用Velocity 1.7作为模板引擎,几天用下来,感觉还是挺爽的。不过今天发现一个坑:macro里面如果有多行的话,输出的字符串前后都被加上了换行和空格。直接上代码:

#macro(getContextPath)
    #set($root =  $context.webApplicationContext.servletContext.contextPath)
    #if($root.lastIndexOf('/') == 0)
        #set($root = $root + '/')
    #end
    $root
#end

 

调用:

aa#{getContextPath}aa

 输出:

aa /web/ aa

 中间确实多了空格,我的目的是所有的URL前缀都加上这个context路径,比如:

<a href="#{getContextPath}menu/list">xxx</a>

 最终我看到的结果会是这样:

<a href="#                   /web/ menu/list">xxx</a>

 很明显这不符合我的要求。

 

网上搜了一把,终于在Velocity的WIKI搜到了解决方案,

#pragma once #include <vector> #include <string> class Config { public: Config(); bool loadFromFile(const std::string& filePath); // === 可视化 & 推理阈值 === int show_window; // 窗口显示开关 (0=不显示,1=显示) float conf_threshold; // 检测置信度阈值 [0.0,1.0] float nms_threshold; // NMS 重叠阈值 [0.0,1.0] // === 瞄准 & FOV === int aim_part; // 瞄准部位 (0=头,1=胸 等) float xfov, yfov; // 水平 / 垂直 视野因子 // === 截图方式 === int screenshot_method; // 0=DXGI, 1=Win32 BitBlt // === 卡尔曼滤波参数 === float kalman_Q; // 过程噪声协方差 float kalman_R; // 测量噪声协方差 int kalman_enable; // 0=禁用,1=启用 // === 鼠标控制参数 === int move_type; // 0=WinMove,1=GBIL 等 int aim_key; // 触发键1 (VK code) int aim_key1; // 触发键2 (VK code) int tracking_toggle_key; // 跟踪开启/关闭键 (VK code) int exit_key; // 退出程序键 (VK code) // === 单目标追踪锁定参数 === float lock_threshold; // 距离中心锁定阈值 (像素) int lock_frames; // 锁定前需持续多少帧 // === 平滑 (PID) 参数 === float xkp, xki, xkd; // X 方向 PID 三项系数 float ykp, yki, ykd; // Y 方向 PID 三项系数 // === 类别过滤 === std::vector<int> valid_classes; // 可跟踪类别列表 // === 锚点偏移比 === float anchor_x; // 框内 X 方向锚点比例 [0.0,1.0] float anchor_y; // 框内 Y 方向锚点比例 [0.0,1.0] // === 多目标跟踪 (MOT) 关联参数 === float iou_threshold; // IOU 关联阈值 [0.0,1.0] int max_lost_frames; // Track 丢失后允许的最大未匹配帧数 int min_hits; // Track 在被认为“有效”前需连续命中帧数 // === 预测系统参数 === int prediction_enable; // 0=禁用, 1=启用 float prediction_coefficient; // 预测系数 int prediction_history_size; // 历史记录数量 int system_delay; // 系统延迟(毫秒) float min_velocity_threshold; // 最小速度阈值 // === ADRC控制器参数(新增) === float adrc_w0; // 观测器带宽 float adrc_b0; // 控制增益 float adrc_wn; // 闭环带宽 float adrc_sigma; // 阻尼系数 };config.h代码和 #include "config.h" #include <windows.h> #include <cstring> Config::Config() { // ---- 默认值 ---- show_window = 1; conf_threshold = 0.4f; nms_threshold = 0.5f; aim_part = 1; xfov = 1.0f; yfov = 1.33f; screenshot_method = 0; kalman_Q = 0.0001f; kalman_R = 10.0f; kalman_enable = 1; move_type = 0; aim_key = 0x10; // Shift aim_key1 = 0x02; // 鼠标右键 tracking_toggle_key = 0x24; // Home exit_key = 0x23; // End lock_threshold = 50.0f; lock_frames = 5; xkp = 0.8f; xki = 0.005f; xkd = 0.15f; ykp = 0.8f; yki = 0.005f; ykd = 0.15f; anchor_x = 0.5f; anchor_y = 0.5f; // MOT 新增部分 iou_threshold = 0.3f; max_lost_frames = 5; min_hits = 3; // 预测系统默认值 prediction_enable = 1; prediction_coefficient = 0.8f; prediction_history_size = 10; system_delay = 50; min_velocity_threshold = 50.0f; // === ADRC默认参数(新增) === adrc_w0 = 50.0f; // 观测器带宽 adrc_b0 = 0.8f; // 控制增益 adrc_wn = 50.0f; // 闭环带宽 adrc_sigma = 1.0f; // 阻尼系数 } bool Config::loadFromFile(const std::string& filePath) { // ---- WINDOWS ---- show_window = GetPrivateProfileIntA("WINDOWS", "show_windows", show_window, filePath.c_str()); // ---- PRED ---- conf_threshold = GetPrivateProfileIntA("PRED", "conf_threshold", static_cast<int>(conf_threshold * 100), filePath.c_str()) / 100.0f; nms_threshold = GetPrivateProfileIntA("PRED", "nms_threshold", static_cast<int>(nms_threshold * 100), filePath.c_str()) / 100.0f; aim_part = GetPrivateProfileIntA("PRED", "aim_part", aim_part, filePath.c_str()); xfov = GetPrivateProfileIntA("PRED", "xfov", static_cast<int>(xfov * 100), filePath.c_str()) / 100.0f; yfov = GetPrivateProfileIntA("PRED", "yfov", static_cast<int>(yfov * 100), filePath.c_str()) / 100.0f; screenshot_method = GetPrivateProfileIntA("PRED", "Screenshot_method", screenshot_method, filePath.c_str()); // ---- 锚点 ---- anchor_x = GetPrivateProfileIntA("PRED", "anchor_x", static_cast<int>(anchor_x * 100), filePath.c_str()) / 100.0f; anchor_y = GetPrivateProfileIntA("PRED", "anchor_y", static_cast<int>(anchor_y * 100), filePath.c_str()) / 100.0f; // ---- 类别过滤 ---- valid_classes.clear(); { char buffer[256]; GetPrivateProfileStringA("PRED", "track_class", "", buffer, sizeof(buffer), filePath.c_str()); char* next = nullptr; char* token = strtok_s(buffer, ",", &next); while (token) { valid_classes.push_back(std::atoi(token)); token = strtok_s(nullptr, ",", &next); } } // ---- KALMAN ---- kalman_Q = GetPrivateProfileIntA("KALMAN", "process_noise", static_cast<int>(kalman_Q * 1e6), filePath.c_str()) / 1e6f; kalman_R = GetPrivateProfileIntA("KALMAN", "obs_noise", static_cast<int>(kalman_R * 100), filePath.c_str()) / 100.0f; kalman_enable = GetPrivateProfileIntA("KALMAN", "enable", kalman_enable, filePath.c_str()); // ---- MOUSE & CONTROL ---- move_type = GetPrivateProfileIntA("MOUSE", "move_type", move_type, filePath.c_str()); aim_key = GetPrivateProfileIntA("CONTROL", "aim_key", aim_key, filePath.c_str()); aim_key1 = GetPrivateProfileIntA("CONTROL", "aim_key1", aim_key1, filePath.c_str()); tracking_toggle_key = GetPrivateProfileIntA("CONTROL", "tracking_toggle_key", tracking_toggle_key, filePath.c_str()); exit_key = GetPrivateProfileIntA("CONTROL", "exit_key", exit_key, filePath.c_str()); // ---- TRACK (单目标) ---- lock_threshold = static_cast<float>(GetPrivateProfileIntA("TRACK", "lock_threshold", static_cast<int>(lock_threshold), filePath.c_str())); lock_frames = GetPrivateProfileIntA("TRACK", "lock_frames", lock_frames, filePath.c_str()); // ---- SMOOTH (PID) ---- xkp = GetPrivateProfileIntA("SMOOTH", "xkp", static_cast<int>(xkp * 100), filePath.c_str()) / 100.0f; xki = GetPrivateProfileIntA("SMOOTH", "xki", static_cast<int>(xki * 1000), filePath.c_str()) / 1000.0f; xkd = GetPrivateProfileIntA("SMOOTH", "xkd", static_cast<int>(xkd * 100), filePath.c_str()) / 100.0f; ykp = GetPrivateProfileIntA("SMOOTH", "ykp", static_cast<int>(ykp * 100), filePath.c_str()) / 100.0f; yki = GetPrivateProfileIntA("SMOOTH", "yki", static_cast<int>(yki * 1000), filePath.c_str()) / 1000.0f; ykd = GetPrivateProfileIntA("SMOOTH", "ykd", static_cast<int>(ykd * 100), filePath.c_str()) / 100.0f; // ---- 预测系统参数 ---- prediction_enable = GetPrivateProfileIntA("PREDICTION", "prediction_enable", prediction_enable, filePath.c_str()); prediction_coefficient = GetPrivateProfileIntA("PREDICTION", "prediction_coefficient", static_cast<int>(prediction_coefficient * 100), filePath.c_str()) / 100.0f; prediction_history_size = GetPrivateProfileIntA("PREDICTION", "prediction_history_size", prediction_history_size, filePath.c_str()); system_delay = GetPrivateProfileIntA("PREDICTION", "system_delay", system_delay, filePath.c_str()); min_velocity_threshold = static_cast<float>(GetPrivateProfileIntA("PREDICTION", "min_velocity_threshold", static_cast<int>(min_velocity_threshold), filePath.c_str())); // ---- MOT 关联配置 ---- iou_threshold = GetPrivateProfileIntA("MOT", "iou_threshold", static_cast<int>(iou_threshold * 100), filePath.c_str()) / 100.0f; max_lost_frames = GetPrivateProfileIntA("MOT", "max_lost_frames", max_lost_frames, filePath.c_str()); min_hits = GetPrivateProfileIntA("MOT", "min_hits", min_hits, filePath.c_str()); // === ADRC参数加载(修改后) === char buffer[32]; if (GetPrivateProfileStringA("ADRC", "w0", "", buffer, sizeof(buffer), filePath.c_str())) { adrc_w0 = std::stof(buffer); } if (GetPrivateProfileStringA("ADRC", "b0", "", buffer, sizeof(buffer), filePath.c_str())) { adrc_b0 = std::stof(buffer); } if (GetPrivateProfileStringA("ADRC", "wn", "", buffer, sizeof(buffer), filePath.c_str())) { adrc_wn = std::stof(buffer); } if (GetPrivateProfileStringA("ADRC", "sigma", "", buffer, sizeof(buffer), filePath.c_str())) { adrc_sigma = std::stof(buffer); } // 打印ADRC参数加载结果 printf("配置文件读取-ADRC参数: w0=%.1f, b0=%.2f, wn=%.1f, sigma=%.1f\n", adrc_w0, adrc_b0, adrc_wn, adrc_sigma); return true; }config.cpp代码,把修改后的完整代码发我 因为主程序的 // ==== 初始化控制器 ==== PIDController pid_x(config.xkp, config.xki, config.xkd); PIDController pid_y(config.ykp, config.yki, config.ykd); // ===== 新增:滑模控制器(解决动态目标跟踪延迟)===== SlidingModeController smc_x(config.smc_k, config.smc_lambda, config.smc_epsilon); SlidingModeController smc_y(config.smc_k, config.smc_lambda, config.smc_epsilon); // ===== 新增:轨迹管理器(解决目标丢失拖影)===== TrajectoryManager trajectory_mgr(config.max_misses, config.decay_factor); KalmanFilter kf(config.kalman_Q, config.kalman_R); printf("主程序-控制参数: PID X[%.2f, %.3f, %.2f] Y[%.2f, %.3f, %.2f]\n", config.xkp, config.xki, config.xkd, config.ykp, config.yki, config.ykd); printf("主程序-滑模参数: k=%.1f, λ=%.1f, ε=%.2f\n", config.smc_k, config.smc_lambda, config.smc_epsilon);和 // 应用移动(带平滑处理) mouse->updateTarget(final_dx * config.sensitivity, final_dy * config.sensitivity);报凑
最新发布
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值