Consistently parse workspace numbers
Patch status: merged
Patch by Tony Crisci
Long description:
Use ws_name_to_number() to parse workspace numbers where this transformation takes place.
To apply this patch, use:
curl http://cr.i3wm.org/patch/601/raw.patch | git am
b/src/commands.c
16 |
@@ -550,12 +550,9 @@ void cmd_move_con_to_workspace_number(I3_CMD, char *which) { |
17 |
/* get the workspace */ |
18 |
Con *output, *workspace = NULL; |
19 |
|
20 |
- char *endptr = NULL; |
21 |
- long parsed_num = strtol(which, &endptr, 10); |
22 |
- if (parsed_num == LONG_MIN || |
23 |
- parsed_num == LONG_MAX || |
24 |
- parsed_num < 0 || |
25 |
- endptr == which) { |
26 |
+ long parsed_num = ws_name_to_number(which); |
27 |
+ |
28 |
+ if (parsed_num == -1) { |
29 |
LOG("Could not parse initial part of \"%s\" as a number.\n", which); |
30 |
// TODO: better error message |
31 |
yerror("Could not parse number"); |
32 |
@@ -954,16 +951,12 @@ void cmd_workspace(I3_CMD, char *which) { |
33 |
void cmd_workspace_number(I3_CMD, char *which) { |
34 |
Con *output, *workspace = NULL; |
35 |
|
36 |
- char *endptr = NULL; |
37 |
- long parsed_num = strtol(which, &endptr, 10); |
38 |
- if (parsed_num == LONG_MIN || |
39 |
- parsed_num == LONG_MAX || |
40 |
- parsed_num < 0 || |
41 |
- endptr == which) { |
42 |
+ long parsed_num = ws_name_to_number(which); |
43 |
+ |
44 |
+ if (parsed_num == -1) { |
45 |
LOG("Could not parse initial part of \"%s\" as a number.\n", which); |
46 |
// TODO: better error message |
47 |
yerror("Could not parse number"); |
48 |
- |
49 |
return; |
50 |
} |
51 |
|
52 |
@@ -1925,15 +1918,8 @@ void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) { |
53 |
/* Change the name and try to parse it as a number. */ |
54 |
FREE(workspace->name); |
55 |
workspace->name = sstrdup(new_name); |
56 |
- char *endptr = NULL; |
57 |
- long parsed_num = strtol(new_name, &endptr, 10); |
58 |
- if (parsed_num == LONG_MIN || |
59 |
- parsed_num == LONG_MAX || |
60 |
- parsed_num < 0 || |
61 |
- endptr == new_name) |
62 |
- workspace->num = -1; |
63 |
- else |
64 |
- workspace->num = parsed_num; |
65 |
+ |
66 |
+ workspace->num = ws_name_to_number(new_name); |
67 |
LOG("num = %d\n", workspace->num); |
68 |
|
69 |
/* By re-attaching, the sort order will be correct afterwards. */ |
b/src/workspace.c
74 |
@@ -177,15 +177,7 @@ Con *create_workspace_on_output(Output *output, Con *content) { |
75 |
if (!exists) { |
76 |
/* Set ->num to the number of the workspace, if the name actually |
77 |
* is a number or starts with a number */ |
78 |
- char *endptr = NULL; |
79 |
- long parsed_num = strtol(ws->name, &endptr, 10); |
80 |
- if (parsed_num == LONG_MIN || |
81 |
- parsed_num == LONG_MAX || |
82 |
- parsed_num < 0 || |
83 |
- endptr == ws->name) |
84 |
- ws->num = -1; |
85 |
- else |
86 |
- ws->num = parsed_num; |
87 |
+ ws->num = ws_name_to_number(ws->name); |
88 |
LOG("Used number %d for workspace with name %s\n", ws->num, ws->name); |
89 |
|
90 |
break; |