-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalpha.php
More file actions
127 lines (119 loc) · 3.03 KB
/
alpha.php
File metadata and controls
127 lines (119 loc) · 3.03 KB
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
<?
error_reporting(E_ALL);
ini_set("display_errors","on");
if ($_POST) {
$text = $_POST["unsorted"];
} else {
$text = "";
}
$output = "";
$in_declaration = false;
$in_comment = false;
$last_char = false;
$length = strlen($text);
$cache = "";
$properties = array();
$property_names = array();
$current_property_name = "";
$hadcomment = false;
$in_property = false;
$in_name = false;
for ($i = 0; $i < $length; $i++) {
$char = substr($text,$i,1);
$l_char = substr($text,$i - 1,1); // for closing comments more reliably
$n_char = substr($text,$i + 1,1); // for opening comments and adding trailing ;s
if ($char == "/" && $n_char == "*") {
$in_comment = true;
$hadcomment = true;
if (!$in_declaration) {
$output .= $char;
}
} elseif ($char == "/" && $l_char == "*") {
$in_comment = false;
if (!$in_declaration) {
$output .= $char;
}
} elseif ($in_comment) {
if (!$in_declaration) {
$output .= $char;
}
} elseif ($char == "{") {
$in_property = false;
$in_name = true;
$in_declaration = true;
$properties = array();
$property_names = array();
$property_errors = array();
} elseif ($char == "}") {
$in_declaration = false;
$in_property = false;
$in_name = false;
// Alphabetize
array_multisort($property_names,$properties,SORT_ASC,SORT_STRING);
$output .= "{ ";
foreach ($properties as $name => $value) {
$output .= $name.": ".$value."; ";
}
if ($property_errors) {
$output .= " /*";
$errors = 0;
$error_count = count($property_errors);
if ($error_count == 1) {
$error_report = "Duplicate Property: ";
} else {
$error_report = "Duplicate Properties: ";
}
foreach ($property_errors as $pe) {
$errors++;
$output .= " " . $pe;
if ($error_count > 1 && $errors < $error_count) {
$output .= ", ";
}
}
$output .= " */ ";
}
$output .= "}";
} elseif ($in_declaration && $char == ":") {
$in_property = true;
$in_name = false;
// This is a property name
if (!in_array(trim($cache),$property_names)) {
$property_names[] = trim($cache);
} else {
// if this is a duplicate
$property_errors[] = trim($cache);
}
$current_property_name = trim($cache);
$cache = "";
} elseif ($in_declaration && $char == ";") {
$in_property = false;
$in_name = true;
$properties[$current_property_name] = trim($cache);
$cache = "";
} elseif ($in_declaration && $n_char == "}") {
// Add trailing ";" if it's not there
$cache .= $char;
if ($in_property) {
$properties[$current_property_name] = trim($cache);
$cache = "";
}
} elseif ($in_declaration) {
$cache .= $char;
} else {
$output .= $char;
}
}
?>
<html>
<head></head>
<body>
<form method="post">
Unsorted<br />
<textarea name="unsorted" rows="20" cols="100"></textarea>
<br /><br>
<input type="submit" value="Submit" />
</form>
Sorted<br />
<textarea name="sorted" rows="20" cols="100"><?=htmlspecialchars($output)?></textarea>
</body>
</html>