• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 195

09 Oct 2023 10:08PM UTC coverage: 63.447% (+0.006%) from 63.441%
195

Pull #1079

circleci

Frotty
small performance fixes
Pull Request #1079: small performance fixes

17146 of 27024 relevant lines covered (63.45%)

0.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.76
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/jassinterpreter/providers/StringProvider.java
1
package de.peeeq.wurstio.jassinterpreter.providers;
2

3
import de.peeeq.wurstio.jassinterpreter.InterpreterException;
4
import de.peeeq.wurstscript.WLogger;
5
import de.peeeq.wurstscript.intermediatelang.ILconstBool;
6
import de.peeeq.wurstscript.intermediatelang.ILconstInt;
7
import de.peeeq.wurstscript.intermediatelang.ILconstReal;
8
import de.peeeq.wurstscript.intermediatelang.ILconstString;
9
import de.peeeq.wurstscript.intermediatelang.interpreter.AbstractInterpreter;
10
import net.moonlightflower.wc3libs.misc.StringHash;
11
import org.apache.commons.lang.StringUtils;
12

13
import java.io.UnsupportedEncodingException;
14
import java.math.RoundingMode;
15
import java.text.NumberFormat;
16
import java.util.Locale;
17
import java.util.regex.Matcher;
18
import java.util.regex.Pattern;
19

20
public class StringProvider extends Provider {
21

22
    public StringProvider(AbstractInterpreter interpreter) {
23
        super(interpreter);
1✔
24
    }
1✔
25

26
    public ILconstString I2S(ILconstInt i) {
27
        return new ILconstString("" + i.getVal());
1✔
28
    }
29

30
    private static final Pattern s2ipattern = Pattern.compile("([+\\-]?[0-9]+).*");
1✔
31
    public ILconstInt S2I(ILconstString s) {
32
        String str = s.getVal();
1✔
33
        Matcher matcher = s2ipattern.matcher(str);
1✔
34
        if (matcher.matches()) {
1✔
35
            str = matcher.group(1);
1✔
36
            return new ILconstInt(Integer.parseInt(str));
1✔
37
        } else {
38
            return new ILconstInt(0);
1✔
39
        }
40
    }
41

42
    private static final Pattern s2rpattern = Pattern.compile("([+\\-]?[0-9]+(\\.[0-9]*)?).*");
1✔
43
    public ILconstReal S2R(ILconstString s) {
44
        String str = s.getVal();
1✔
45

46
        Matcher matcher = s2rpattern.matcher(str);
1✔
47
        if (matcher.matches()) {
1✔
48
            str = matcher.group(1);
1✔
49
            return new ILconstReal(Float.parseFloat(str));
1✔
50
        } else {
51
            return new ILconstReal(0);
1✔
52
        }
53
    }
54

55
    public ILconstString R2S(ILconstReal r) {
56
        return new ILconstString("" + r.getVal());
1✔
57
    }
58

59
    public ILconstString R2SW(ILconstReal r, ILconstInt width, ILconstInt precision) {
60
        NumberFormat formatter = NumberFormat.getInstance(Locale.US);
1✔
61
        formatter.setMaximumFractionDigits(precision.getVal());
1✔
62
        formatter.setMinimumFractionDigits(precision.getVal());
1✔
63
        formatter.setRoundingMode(RoundingMode.HALF_UP);
1✔
64
        formatter.setGroupingUsed(false);
1✔
65
        String s = formatter.format(r.getVal());
1✔
66
        // pad to desired width
67
        s = StringUtils.rightPad(s, width.getVal());
1✔
68
        return new ILconstString(s);
1✔
69
    }
70

71
    public ILconstInt R2I(ILconstReal i) {
72
        return new ILconstInt((int) i.getVal());
1✔
73
    }
74

75
    public ILconstReal I2R(ILconstInt i) {
76
        return new ILconstReal(i.getVal());
×
77
    }
78

79

80
    public ILconstInt StringHash(ILconstString s) {
81
        if (s == null) {
1✔
82
            return new ILconstInt(0);
×
83
        }
84
        try {
85
            return new ILconstInt(StringHash.hash(s.getVal()));
1✔
86
        } catch (UnsupportedEncodingException e) {
×
87
            WLogger.severe(e);
×
88
        }
89
        return new ILconstInt(0);
×
90
    }
91

92
    public ILconstInt StringLength(ILconstString string) {
93
        return new ILconstInt(string.getVal().length());
1✔
94
    }
95

96
    public ILconstString SubString(ILconstString istr, ILconstInt start, ILconstInt end) {
97
        String str = istr.getVal();
1✔
98
        int s = start.getVal();
1✔
99
        if (s < 0) {
1✔
100
            // I am not gonna emulate the WC3 bug for negative indexes here ...
101
            throw new InterpreterException("SubString called with negative start index: " + start);
×
102
        }
103
        int e = end.getVal();
1✔
104
        if (e >= str.length()) {
1✔
105
            // Warcraft does no bound checking here:
106
            e = str.length();
1✔
107
        }
108
        if (s > str.length()) {
1✔
109
            // if start is above string length, wc3 will return null
110
            // since this is most likely a bug in your code, the interpreter will throw an exception instead:
111
            throw new InterpreterException("SubString called with start index " + start + " greater than string length " + str.length());
1✔
112
        }
113
        return new ILconstString(str.substring(s, e));
1✔
114
    }
115

116
    public ILconstString StringCase(ILconstString string, ILconstBool upperCase) {
117
        return new ILconstString(
1✔
118
                upperCase.getVal() ?
1✔
119
                        string.getVal().toUpperCase()
1✔
120
                        : string.getVal().toLowerCase());
1✔
121
    }
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc