Count the Bytes!
C#, 249 270 bytes
u=>{var c=new System.Net.Http.HttpClient().GetStringAsync(u).Result;var i=c.IndexOf("de>",c.IndexOf('"'+u.Split('/')[4]))+3;return System.Text.Encoding.UTF8.GetByteCount(c.Substring(i,c.IndexOf("<",i)-i-1).Replace(">",">").Replace("<","<").Replace("&","&"));};
I've made some assumptions based on observations of the HTML of pages. Hopefully they hold true for all answer posts.
+21 bytes to unencode &
to &
. My answer failed to count it's own bytes. ;P
Ungolfed:
/*Func<string, int> Lambda =*/ u =>
{
// Download HTML of the page.
// Although new System.New.WebClient().DownloadString(u) looks shorter, it doesn't
// preserve the UTF8 encoding.
var c = new System.Net.Http.HttpClient().GetStringAsync(u).Result;
// Using the answer id from the URL, find the index of the start of the code block.
// Empirical observation has found that all code blocks are in <code> tags,
// there are no other HTML tags that end with "de>",
// and that '>' and '<' are encoded to '>'/'<' so no jerk can put "de>"
// before the first code block.
var i = c.IndexOf("de>", c.IndexOf('"' + u.Split('/')[4])) + 3;
// Get the substring of the code block text, unencode '>'/'<'/'&' and get the byte count.
// Again, empirical observation shows the closing </code> tag is always on a new
// line, so always remove 1 character when getting the substring.
return System.Text.Encoding.UTF8.GetByteCount(c.Substring(i, c.IndexOf("<", i) - i - 1).Replace(">", ">").Replace("<", "<")..Replace("&", "&"));
};
Results:
Input: http://codegolf.stackexchange.com/a/91904/30525
Output: 34
Input: http://codegolf.stackexchange.com/a/91693/30525
Output: 195 (at the time of this answer the first post has 196, but I can't find the 196th
byte, even counting by hand, so assuming 195 is correct)
Input: http://codegolf.stackexchange.com/a/80691/30525
Output: 61
Input: http://codegolf.stackexchange.com/a/91995/58106 (this answer)
Output: 270
jQuery JavaScript, 97 Bytes
console.log((new Blob([$("#answer-"+location.hash.substr(1)+" code").eq(0).text().trim()])).size)
old version 151 Bytes
s=$('#answer-'+location.hash.substr(1)+' code').eq(0).text().trim();c=0;for(i=s.length;i--;)d=s.charCodeAt(i),c=d<128?c+1:d<2048?c+2:c+3;console.log(c)
old version 163 Bytes
s=$('#answer-'+location.hash.substr(1)+' code').first().text().trim();c=0;for(i=0;i<s.length;i++){d=s.charCodeAt(i);c=(d<128)?c+1:(d<2048)?c+2:c+3;}console.log(c);
input ist location and jQuery is active on the site
Java 7, 314 313 bytes
int c(String u)throws Exception{String x="utf8",s=new java.util.Scanner(new java.net.URL(u).openStream(),x).useDelimiter("\\A").next();int j=s.indexOf("de>",s.indexOf('"'+u.split("/")[4]))+3;return s.substring(j,s.indexOf("<",j)-1).replace(">",">").replace("<","<").replace("&","&").getBytes(x).length;}
Shamelessly stolen from @milk's C# answer and ported to Java 7.
NOTE: This assumes all code is in <code>
blocks. Currently it won't work with just <pre>
tags (but who uses those anyway?.. ;p).
Ungolfed & test cases:
class M{
static int c(String u) throws Exception{
String x = "utf8",
s = new java.util.Scanner(new java.net.URL(u).openStream(), x).useDelimiter("\\A").next();
int j = s.indexOf("de>", s.indexOf('"'+u.split("/")[4])) + 3;
return s.substring(j, s.indexOf("<", j) - 1).replace(">", ">").replace("<", "<").replace("&", "&")
.getBytes(x).length;
}
public static void main(String[] a) throws Exception{
System.out.println(c("https://codegolf.stackexchange.com/a/91904/30525"));
System.out.println(c("https://codegolf.stackexchange.com/a/91693/30525"));
System.out.println(c("https://codegolf.stackexchange.com/a/80691/30525"));
System.out.println(c("https://codegolf.stackexchange.com/a/91995/58106"));
}
}
Output:
34
195
61
270