// XML helpers. `escape` replaces XML special characters with entity
// references — used when emitting Info.plist / AndroidManifest content
// from sx values that may contain user-supplied text.
#import "modules/std.sx";

escape :: (s: string) -> string {
    result := "";
    i := 0;
    seg_start := 0;
    while i < s.len {
        c := s[i];
        // 38='&', 60='<', 62='>', 34='"', 39='\''
        ent := "";
        if c == 38 { ent = "&amp;"; }
        if c == 60 { ent = "&lt;"; }
        if c == 62 { ent = "&gt;"; }
        if c == 34 { ent = "&quot;"; }
        if c == 39 { ent = "&apos;"; }
        if ent.len > 0 {
            if i > seg_start {
                result = concat(result, substr(s, seg_start, i - seg_start));
            }
            result = concat(result, ent);
            seg_start = i + 1;
        }
        i += 1;
    }
    if seg_start < s.len {
        result = concat(result, substr(s, seg_start, s.len - seg_start));
    }
    result
}
