File size: 10,904 Bytes
037f593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ccee15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE html>
<html lang="en-GB">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SMS Segment Counter</title>
<style>
:root{--bg:#0A0E14;--panel:#111722;--line:#1E2938;--ink:#E8EDF5;--muted:#93A1B5;--brass:#E8C87E;--brass-dim:#C9A85C;--bad:#F08A7E;--ok:#8FD3A5}
*{box-sizing:border-box}
body{margin:0;background:var(--bg);color:var(--ink);font:15px/1.6 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;padding:32px 16px 64px}
.wrap{max-width:860px;margin:0 auto}
.top{display:flex;justify-content:space-between;align-items:center;gap:12px;margin-bottom:22px;font-size:13px;color:var(--muted)}
.top a{color:var(--brass);text-decoration:none}
h1{font-size:26px;margin:0 0 6px;letter-spacing:-0.01em}
h2{font-size:15px;margin:0 0 14px;color:var(--brass);text-transform:uppercase;letter-spacing:.08em}
p{color:var(--muted);margin:0 0 14px}
.lede{color:var(--ink);font-size:16px}
.card{background:var(--panel);border:1px solid var(--line);border-radius:12px;padding:20px;margin:18px 0}
.row{display:flex;flex-wrap:wrap;gap:12px;align-items:center}
.grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:16px}
button{background:var(--brass);color:#14100A;border:0;border-radius:8px;padding:10px 16px;font-weight:600;font-size:14px;cursor:pointer}
button.ghost{background:transparent;color:var(--brass);border:1px solid var(--brass-dim)}
button:disabled{opacity:.45;cursor:not-allowed}
label{display:block;font-size:13px;color:var(--muted);margin-bottom:6px}
select,input[type=text],textarea{background:#0C121C;color:var(--ink);border:1px solid var(--line);border-radius:8px;padding:9px 10px;font-size:14px;width:100%}
textarea{min-height:120px;resize:vertical;font-family:inherit}
input[type=range]{width:100%;accent-color:var(--brass)}
.val{color:var(--brass);font-variant-numeric:tabular-nums}
.stat{background:#0C121C;border:1px solid var(--line);border-radius:10px;padding:12px 14px}
.stat .k{font-size:12px;color:var(--muted);text-transform:uppercase;letter-spacing:.06em}
.stat .v{font-size:22px;font-weight:600;color:var(--ink);font-variant-numeric:tabular-nums}
.stat .v.bad{color:var(--bad)} .stat .v.ok{color:var(--ok)}
.note{font-size:13px;color:var(--muted)}
.bullets{color:var(--muted);padding-left:18px;margin:0 0 6px}
.bullets li{margin-bottom:8px}
a{color:var(--brass)}
footer{margin-top:28px;font-size:13px;color:var(--muted)}
code,.mono{font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:13px}
.pill{display:inline-block;padding:2px 8px;border-radius:999px;border:1px solid var(--line);font-size:12px;color:var(--muted);margin:2px 4px 2px 0}
table{width:100%;border-collapse:collapse;font-size:14px}
th,td{text-align:left;padding:8px 6px;border-bottom:1px solid var(--line)}
th{color:var(--muted);font-weight:500;font-size:12px;text-transform:uppercase;letter-spacing:.06em}

.seg{display:inline-block;background:#0C121C;border:1px solid var(--line);border-radius:8px;padding:8px 10px;margin:4px 6px 4px 0;font-size:13px;max-width:100%;white-space:pre-wrap;word-break:break-word}
.seg b{color:var(--brass);font-weight:600;margin-right:6px}
.hl{color:var(--bad);border-bottom:1px dotted var(--bad)}
</style>
</head>
<body>
<div class="wrap">
  <div class="top">
    <span>PacketExchange developer tools</span>
    <a href="https://huggingface.co/PacketExchange">More tools</a>
  </div>

  <h1>SMS Segment Counter</h1>
  <p class="lede">Know how many segments a message will bill before you send it, and which character flipped it to Unicode.</p>
  <p>Type or paste a message. Counting is done in your browser against the GSM 03.38 character set, which is what the carrier network actually uses. Nothing is sent anywhere.</p>

  <div class="card">
    <h2>Message</h2>
    <textarea id="msg" placeholder="Your verification code is 482913. It expires in 10 minutes."></textarea>
    <div class="grid" style="margin-top:14px">
      <div class="stat"><div class="k">Encoding</div><div class="v" id="enc">GSM-7</div></div>
      <div class="stat"><div class="k">Characters</div><div class="v" id="chars">0</div></div>
      <div class="stat"><div class="k">Segments</div><div class="v" id="segs">0</div></div>
      <div class="stat"><div class="k">Left in this segment</div><div class="v" id="left">160</div></div>
    </div>
    <p class="note" id="why" style="margin-top:12px"></p>
    <div id="fixes"></div>
  </div>

  <div class="card">
    <h2>How it will be split</h2>
    <div id="preview" class="note">Nothing to split yet.</div>
  </div>

  <div class="card">
    <h2>The rules in one place</h2>
    <ul class="bullets">
      <li>GSM-7 fits 160 characters in a single message. Once a message needs more than one segment, each segment carries a 6 byte header and holds 153.</li>
      <li>One character outside the GSM-7 set, a curly quote or an emoji for example, switches the whole message to UCS-2: 70 characters single, 67 per segment after that.</li>
      <li>Twelve GSM-7 characters are "extended" and cost two: <span class="mono">^ { } \ [ ] ~ | €</span> and form feed. A single euro sign can push a 159 character message into two segments.</li>
      <li>Segments are what you are billed for and what the handset reassembles. Long messages also take longer to deliver, because each segment travels separately.</li>
      <li>For one-time codes, keep the whole message in one GSM-7 segment and put the code early. Preview text on a locked phone is short.</li>
    </ul>
  </div>

  <footer>
    Built by <a href="https://huggingface.co/PacketExchange">PacketExchange</a>. If you send SMS or one-time codes through code, the
    <a href="https://packetexchange.io/developers">PacketExchange API</a> does this on wholesale routes with
    <a href="https://github.com/PacketExchangeIO">open source clients</a> for Node, Python and more.
    Questions are welcome at <a href="mailto:contact@packetexchange.io">contact@packetexchange.io</a>.
  </footer>
</div>

<script>
(function(){
  "use strict";
  var BASIC = "@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà";
  var EXT = "^{}\\[~]|€\f";
  var basicSet = {}, extSet = {};
  for(var i=0;i<BASIC.length;i++) basicSet[BASIC[i]] = true;
  for(var j=0;j<EXT.length;j++) extSet[EXT[j]] = true;

  var SUGGEST = {
    "‘":"'", "’":"'", "‚":"'", "“":"\"", "”":"\"", "„":"\"",
    "–":"-", "—":"-", "…":"...", " ":" ", "•":"-", "´":"'", "′":"'",
    "«":"\"", "»":"\"", "™":"TM", "®":"(R)", "©":"(C)", "−":"-", "×":"x"
  };

  var els = {
    msg: document.getElementById('msg'), enc: document.getElementById('enc'),
    chars: document.getElementById('chars'), segs: document.getElementById('segs'),
    left: document.getElementById('left'), why: document.getElementById('why'),
    fixes: document.getElementById('fixes'), preview: document.getElementById('preview')
  };

  function codepoints(s){ return Array.from(s); }

  function analyse(text){
    var cps = codepoints(text), bad = [], units = 0, k;
    var isGsm = true;
    for(k=0;k<cps.length;k++){
      var ch = cps[k];
      if(basicSet[ch]) units += 1;
      else if(extSet[ch]) units += 2;
      else { isGsm = false; if(bad.indexOf(ch) < 0) bad.push(ch); }
    }
    var res = {isGsm:isGsm, bad:bad, count:cps.length};
    if(isGsm){
      res.units = units;
      res.single = 160; res.multi = 153;
      res.segments = units === 0 ? 0 : (units <= 160 ? 1 : Math.ceil(units / 153));
    } else {
      // UCS-2 counts UTF-16 code units; astral characters (most emoji) cost two
      var u16 = 0;
      for(k=0;k<cps.length;k++) u16 += cps[k].length;
      res.units = u16;
      res.single = 70; res.multi = 67;
      res.segments = u16 === 0 ? 0 : (u16 <= 70 ? 1 : Math.ceil(u16 / 67));
    }
    var per = res.segments <= 1 ? res.single : res.multi;
    res.left = res.segments === 0 ? res.single : (per * res.segments - res.units);
    res.per = per;
    return res;
  }

  function split(text, res){
    var cps = codepoints(text), parts = [], cur = "", used = 0, cap = res.segments <= 1 ? res.single : res.multi;
    for(var k=0;k<cps.length;k++){
      var ch = cps[k], cost;
      if(res.isGsm) cost = extSet[ch] ? 2 : 1; else cost = ch.length;
      if(used + cost > cap){ parts.push(cur); cur = ""; used = 0; }
      cur += ch; used += cost;
    }
    if(cur.length) parts.push(cur);
    return parts;
  }

  function esc(s){ return s.replace(/[&<>]/g, function(c){ return {'&':'&amp;','<':'&lt;','>':'&gt;'}[c]; }); }
  function name(ch){
    var cp = ch.codePointAt(0).toString(16).toUpperCase();
    while(cp.length < 4) cp = "0" + cp;
    return "U+" + cp;
  }

  function render(){
    var text = els.msg.value;
    var r = analyse(text);
    els.enc.textContent = r.isGsm ? "GSM-7" : "UCS-2";
    els.enc.className = "v " + (r.isGsm ? "ok" : "bad");
    els.chars.textContent = r.count + (r.units !== r.count ? " (" + r.units + " units)" : "");
    els.segs.textContent = r.segments;
    els.segs.className = "v " + (r.segments > 1 ? "bad" : "");
    els.left.textContent = r.left;

    if(r.count === 0){ els.why.textContent = ""; els.fixes.innerHTML = ""; els.preview.textContent = "Nothing to split yet."; return; }

    if(r.isGsm){
      els.why.textContent = "Every character is in the GSM-7 set. " + (r.segments === 1 ? "This goes as one segment." : "Over 160, so each segment holds 153.");
      els.fixes.innerHTML = "";
    } else {
      els.why.textContent = "Unicode was forced by " + r.bad.length + " character" + (r.bad.length > 1 ? "s" : "") + " outside GSM-7, so every segment now holds " + r.per + ".";
      var html = "";
      for(var k=0;k<r.bad.length;k++){
        var ch = r.bad[k], s = SUGGEST[ch];
        html += '<span class="pill mono">' + esc(ch) + ' ' + name(ch) + (s !== undefined ? ' &rarr; ' + esc(s) : '') + '</span>';
      }
      var fixed = text;
      for(var key in SUGGEST){ if(SUGGEST.hasOwnProperty(key)) fixed = fixed.split(key).join(SUGGEST[key]); }
      var r2 = analyse(fixed);
      if(r2.isGsm){
        html += '<div style="margin-top:10px"><button class="ghost" id="applyFix">Replace with plain equivalents (' + r2.segments + ' segment' + (r2.segments === 1 ? '' : 's') + ')</button></div>';
      }
      els.fixes.innerHTML = html;
      var btn = document.getElementById('applyFix');
      if(btn) btn.addEventListener('click', function(){ els.msg.value = fixed; render(); });
    }

    var parts = split(text, r), out = "";
    for(var p=0;p<parts.length;p++){
      out += '<div class="seg"><b>' + (p+1) + '/' + parts.length + '</b>' + esc(parts[p]) + '</div>';
    }
    els.preview.innerHTML = out;
  }

  els.msg.addEventListener('input', render);
  render();
})();
</script>
</body>
</html>