Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Thom Yorke raps
Nov 2, 2004


code:
boolean isA = getABoolean();
boolean isB = getAnotherBoolean();
boolean isTrue = !(a^b);
I had a 15 minute discussion with the original coder after sending him a code review with the last line changed to
code:
boolean isTrue = (a == b);
He didn't believe me that the lines were equivalent, even when I showed him the truth tables.

Adbot
ADBOT LOVES YOU

GROVER CURES HOUSE
Aug 26, 2007

Go on...

Ranma posted:

code:
boolean isTrue = !(a^b);

What in the deferred gently caress.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
code:
function sortByID(ob1,ob2) {
	if(!ob1.id || !ob2.id)
		return -1;
	var value1 = parseInt(ob1.id);
	var value2 = parseInt(ob2.id);
	return value1 < value2 ? -1 :
    value1 > value2 ? 1 : 0;
}
What happens if ob1.id or ob2.id equals 0?

shrughes
Oct 11, 2008

(call/cc call/cc)

Wheany posted:

code:
function sortByID(ob1,ob2) {
	if(!ob1.id || !ob2.id)
		return -1;
	var value1 = parseInt(ob1.id);
	var value2 = parseInt(ob2.id);
	return value1 < value2 ? -1 :
    value1 > value2 ? 1 : 0;
}
What happens if ob1.id or ob2.id equals 0?

It works correctly? What's the problem? You mean if it equals "0", right?

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:

shrughes posted:

It works correctly? What's the problem? You mean if it equals "0", right?

ob1.id = 0
ob2.id = "5"

sortById(ob1, ob2) gives -1
sortById(ob2, ob1) gives -1

So ob1 is both lesser AND greater than ob2?

Also:

ob3.id = 0

sortById(ob1, ob3) gives -1
sortById(ob3, ob1) gives -1

Shouldn't they be equal?

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Holy poo poo I feel like I should post daily digests goddamn. Ternary operations are a sign of skill and you should use long chains of them whenever possible to avoid unsightly if - else if chains. :hurr:

But this is even better:

this.val?htmlCode+='&nbsp;&#58;&nbsp;':{};

&#58; is a colon, btw.

zergstain
Dec 15, 2005

Is it not a String at that point? Otherwise, why the call to parseInt()?

Edit: Oh wait, it's JavaScript, not Java. Still shouldn't be a problem if the id is from a DOM element. I think, anyway.

zergstain fucked around with this message at 13:34 on Jun 23, 2011

gold brick
Jun 19, 2001

no he isn't

Aleksei Vasiliev posted:

Strings are immutable and persist in memory for an undefined amount of time, using char arrays and zeroing them after use is proper
For what it's worth, .NET has a facility for just this purpose.

http://msdn.microsoft.com/en-us/library/system.security.securestring.aspx

Computer viking
May 30, 2011
Now with less breakage.

gold brick posted:

For what it's worth, .NET has a facility for just this purpose.

http://msdn.microsoft.com/en-us/library/system.security.securestring.aspx

That's actually ... remarkably sensible of them. I wonder how often it's been badly reinvented.

pseudorandom name
May 6, 2007

The only way to read a SecureString is to copy it to unmanaged memory, so hold your praise.

1337JiveTurkey
Feb 17, 2005

Check out the comments if you want to see the point of that class practically in orbit over some people's heads.

The Saddest Robot
Apr 17, 2007
This is my first time reading about DPAPI after looking at SecureString.

Microsoft posted:

Figure 4, later, shows how all the different keys operate together to allow DPAPI to provide data protection.


:confused::fh:

I think I'll go back to loving around with 3d graphics in XNA.

CoasterMaster
Aug 13, 2003

The Emperor of the Rides


Nap Ghost

suction posted:

has this been posted? Found it on Reddit from 4chan

code:
#!/bin/bash
function f() {
    sleep "$1"
    echo "$1"
}
while [ -n "$1" ]
do
    f "$1" &
    shift
done
wait

example usage:
./sleepsort.bash 5 3 6 3 6 3 1 4 7

Showed this to a co-worker of mine. He decided to turn it in to merge sleep sort:

code:
import sys, os, time

input = [int(x) for x in sys.argv[1:]]
pids = []
while len(input) > 1:
    mid = len(input)/2
    halves = [input[:mid], input[mid:]]
    pid = os.fork()
    forked = True
    if pid:
        pids.append(pid)
        forked = False
    else:
        pids = []
    input = halves[forked]
time.sleep(input[0])
print input[0]

for pid in pids:
    os.waitpid(pid, os.WUNTRACED)

And here's a fun question: is sleepsort stable?

brosmike
Jun 26, 2009

CoasterMaster posted:

And here's a fun question: is sleepsort stable?

It's not even guaranteed to be correct, so, no.

It's still awesome as gently caress though

JediGandalf
Sep 3, 2004

I have just the top prospect YOU are looking for. Whaddya say, boss? What will it take for ME to get YOU to give up your outfielders?

gold brick posted:

For what it's worth, .NET has a facility for just this purpose.

http://msdn.microsoft.com/en-us/library/system.security.securestring.aspx
Wow thank you for pointing that out to me. I'm writing a billing application and I'm bordering on abject paranoia when it comes to security. This is an excellent find and good to know that string objects persist in memory.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

CoasterMaster posted:

And here's a fun question: is sleepsort stable?
Given a sufficiently fast/unloaded processor and precise scheduler it is, and that's pretty much the best kind of correctness guarantee there is.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Considering that sleepsort is just insertion sort with a whole lot of system call overhead chucked in...

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Nope. It's not even very hard to get it to give bogus results:

plorkyeran@valdr:~$ ./sleepsort.bash 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1
1
1
1
1
1
1
1
1
1
0
1
11

0
1
0
1
1
0
0
1
1
10

1
0
1
0
0
00

1
1
0
0
0
0
0
0
0
0
0
0
0
0
1100101010101010101010101010110010101010101010101010

wwb
Aug 17, 2004

Day 2 of 3 of iOS boot camp. My first 2 days with objective-C. I can get past lots of the weird assed syntax. But why the gently caress do you #define boolean true and false as YES and NO?

NotShadowStar
Sep 20, 2000
Wrong thread?

It's to make things more natural language readable. if ([obj isFinished] == YES), and so forth

Mikey-San
Nov 3, 2005

I'm Edith Head!

NotShadowStar posted:

Wrong thread?

It's to make things more natural language readable. if ([obj isFinished] == YES), and so forth

Honestly, if I saw this, I would think it's weird. Usually, this is all people do:

if ([obj isFinished]) { ...

It reads more naturally that way. Where YES/NO reads well is stuff like:

[obj doSomethingWithFile:filePath atomically:YES];

wwb
Aug 17, 2004

Wrong thread? Didn't think so and I didn't think it was appropriate making GBS threads up the Apple Dev megathread.

Yes, the reading angle makes sense, but just about every other language I can think of defines boolean "true" using the keyword TRUE and boolean "false" using the keyword FALSE. Kind of like all the other little apple differences. I should say it is a pretty slick language and environment once you get past the funkiness.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

wwb posted:

just about every other language I can think of defines boolean "true" using the keyword TRUE and boolean "false" using the keyword FALSE.

I actually quite like how YAML defines true, yes, and on all as boolean true.

code:
correctResponse: yes
ignition: on
editable: true

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
It's old ugliness. Objective-C is a C language extension, and C didn't get a native boolean type until C99.

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
Anyway it isn't like you're going to forget which is which. I hope.

lazer_chicken
May 14, 2009

PEW PEW ZAP ZAP
Yeah so I had a desperate need to change/renumber a bunch of html tags. It needed to be quick and dirty but it was complicated so I decided to use awk. This was mistake #1. Have fun.

code:
#!/usr/local/bin/bash
cat $1 | awk 'BEGIN{s=1} {gsub(/<sup>([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])<\/sup>/, \
"<a id\=\"eref1\"href\=\"\#endnote1\"><sup>TEMP<\/sup><\/a>",$0);gsub(/<p style=\"font-size:small\"> \
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).&ensp\;/,"<p style=\"font-size:small\"> \
<a href=\"\#eref1\" id=\"endnote1\">TEMP<\/a>.\\&ensp\;",$0); print $0 > "newfile.html" }'

cat "newfile.html" | awk 'BEGIN{t=20} {gsub("\"\#eref1","\"\#eref"(t-19),$0); \
gsub("\"endnote1\">TEMP","\"endnote"(t-19)"\">"(t-19),$0); if ($0 ~ /\#eref/) t++; print $0 > "newfile1.html" }'

cat "newfile1.html" | awk 'BEGIN{n=20} {for (i=1; i<=NF; i++) {sub("\"eref1","\"eref"(n-19),$i); \
sub("\"\#endnote1\"><sup>TEMP","\"\#endnote"(n-19)"\"><sup>"(n-19),$i); \
if ($i ~ /\#endnote/) n++;} print $0 > "newfile2.html"}'

rm newfile.html newfile1.html
I added some line breaks because there are only 3 in the original. To be fair, these are very specific html files (not for a website) so my careless regexes are, in fact, sure to only match what I want them to. Still a horrible, horrible script. But it works and it saves me a shitload of time.

EDIT: more line breaks to fix the tables

lazer_chicken fucked around with this message at 20:16 on Jun 28, 2011

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

Ryouga Inverse posted:

Anyway it isn't like you're going to forget which is which. I hope.
Unless you're a shell scripter.

tef
May 30, 2004

-> some l-system crap ->

lazer_chicken posted:

Yeah so I had a desperate need to change/renumber a bunch of html tags. It needed to be quick and dirty but it was complicated so I decided to use awk. This was mistake #1. Have fun.

awk owns :krad:

Opinion Haver
Apr 9, 2007

I'd much rather use perl -pe (or -ne) than awk. More like awkward :iceburn:

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
sh (or rc) + awk = all a real man needs.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.
Awk basically would have disappeared years ago if cut was able to handle multi-character delimiters

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Cut is great if you only want to cut out fields, but I usually want to output things in more interesting ways.

Computer viking
May 30, 2011
Now with less breakage.

Misogynist posted:

Awk basically would have disappeared years ago if cut was able to handle multi-character delimiters

And field reordering + duplication - I want to do "cut -d, -f 4,2,3,3", drat it.

tef
May 30, 2004

-> some l-system crap ->
is it wrong to want a version of awk that handles xpath :catdrugs:

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

Mustach posted:

Cut is great if you only want to cut out fields, but I usually want to output things in more interesting ways.
Anything actually interesting is too complicated for awk anyway.

Dooey
Jun 30, 2009
Saw this in a diff during merge:

before:

code:
IPC0bits.T1IP = 2;	// Interrupt priority 2 (low)  //3 (mid-low)
after:

code:
IPC0bits.T1IP = 2;	//interrupt priorty 6 (high)// Interrupt priority 2 (low)  //3 (mid-low)

ivantod
Mar 27, 2010

Mahalo, fuckers.
Regarding all the talk about HTML parsing, just thought I'd mention this for those that don't already know about it:

http://www.crummy.com/software/BeautifulSoup/

Beautiful soup is a Python library for handling HTML parsing/modification. It DOES handle imperfect markup and all that...

Lurchington
Jan 2, 2003

Forums Dragoon
although I'd totally recommend Beautiful Soup over something like regex parsing, I think 'best practices' for this kind of thing is lxml.html

Tad Naff
Jul 8, 2004

I told you you'd be sorry buying an emoticon, but no, you were hung over. Well look at you now. It's not catching on at all!
:backtowork:
php:
<?
$fn = $w['type'] . '_form';
if ($w['is_container']) {
    $ao .= $fn($w, '', $clonedFrom, 'edit');
} else {
    if($w['type']=='image'){
        $ao .= $fn($w, '', $clonedFrom, 'edit');
    }else{
        $ao .= $fn($w, '', $clonedFrom, 'edit');
    }
}
?>
:crossarms:

Adbot
ADBOT LOVES YOU

Thel
Apr 28, 2010

FeloniousDrunk posted:

php:
<?
//php awfulness
?>
:crossarms:

:what: indeed.

e: wait those are two different smilies. Herp.

Anyway, here's one from the files:

code:
SharedUtils.getFirstWord = function(str) {
  if (str.indexOf(' ') < 0) {
    return str;
  }
  for(var i = 0; i <str.length; i++){
    if(str.charAt(i) == ' '){
      return str.substring(0,i);
    }
  }
}

SharedUtils.trim = function(str) {
  str = str.replace(/^\s+/, '');
  for (var i = str.length - 1; i >= 0; i--) {
    if (/\S/.test(str.charAt(i))) {
      str = str.substring(0, i + 1);
      break;
    }
  }
  return str;
}
Coding horrors all up in this place. The best part is these two are used interchangeably, despite only having the same effect in some cases. :v:

Thel fucked around with this message at 23:51 on Jul 4, 2011

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply